Skip to main content
gamedev.net
🔒 Locked

[.net] Modifying KeyEventArgs

Started by Ataru 0 replies 1.6k views
Original Post
Ataru
Ataru
I have an event handler that gets a KeyDown event, and processes some KeyEventArgs modifiers. I'd like to call another event handler that handles all of the un-handled modifiers. I.e. if foo(KeyEventArgs e) handles e.Alt and e.Shift then foobar(KeyEventArgs e) would only see e.Ctrl etc... At first I thought i could set the modifiers to false. i.e. e.Shift = false, but I can't since it's read only. I don't mind creating a new KeyEventArgs and set the modifiers, but I don't know how to do that. The handled flag is fine if you handled the entire event, but not if you partially handled it. Any ideas?
philprice
philprice
I'm not really sure what you are asking, if you don't want "Shift" to see "Ctrl" this is not possible if both keys are down, it's more of a logic problem, but anyway, what I would do...

Use multiple event handlers subscribed to the event, which check the state of the keys they care about, and only handle them if they care about them.

A bit of pseudo-esq code

... Foo.KeyDown += new KeyEventHandler(foo);Foo.KeyDown += new KeyEventHandler(bar);...private void foo(object sender, KeyEventArgs e){   if(e.Shift || e.Alt)   {      Console.WriteLine("ZOMG SHIFT or ALT!");   }}private void bar(object sender, KeyEventArgs e){   if(e.Ctrl)   {      Console.WriteLine("Ctrl is down");    }}


Bear in mind this could be inefficient and I may or may not have misunderstood your question, if you do use this method don't forget to remove the delegates on the event when you are done listening.

Just a side point, obviously i'm not sure what your aim is (it would be good if you stated it), if you are *doing* things on key down i would suggest that isnt that wise because it may fire multiple times when the key is down, i usually temp to create a stack of keys + modifers, and set and unset the state of keys on the state / modifers which is then processed by my Update(); method(s) - as it's an event it can fire anytime when the message pump decides it's time to pump.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.