? The Key Events
-
[mode="frustrated"] ARGH ! [/mode] Please, please, if you can help with this, *please* do so: I'm about to loose my mind on it :-) Here's the problem: I'm developing a simple WinForm application. Among other things, I'd like to set a private boolean flag, called "shiftPressed" to true when the user is pressing the Shift key. This flag is checked when doing other things (such as clicking on the form) to modify the behavior of the application as needed. So, I figured the logical thing to do was to intercept the KeyDown and the KeyUp events. In the Key down event, I would set the flag to true if the Shift key is pressed; in the KeyUp event I would unset the flag as needed. However, it seems that if I catch the KeyDown, the KeyUp event is not fired, and/or vice-versa (and let's not even talk about the KeyPressed event !). In short, I have
private void myApp_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyDown"); e.Handled=false; } private void myApp_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyUp"); }
and I see only "KeyDown" message boxes. Note that the line e.handled=false; can be there or not, but the behavior seems to be always the same. What can I do? Do I have to dive intopublic class TestMessageFilter: IMessageFilter {...}
andpublic bool HandleKeys(Keys keyCode) { bool ret = true; switch(keyCode) { case Keys.Shift: ...do stuff... default: ret = false; break; } return ret; }
??? Thanks in advance, F.O.R. -
[mode="frustrated"] ARGH ! [/mode] Please, please, if you can help with this, *please* do so: I'm about to loose my mind on it :-) Here's the problem: I'm developing a simple WinForm application. Among other things, I'd like to set a private boolean flag, called "shiftPressed" to true when the user is pressing the Shift key. This flag is checked when doing other things (such as clicking on the form) to modify the behavior of the application as needed. So, I figured the logical thing to do was to intercept the KeyDown and the KeyUp events. In the Key down event, I would set the flag to true if the Shift key is pressed; in the KeyUp event I would unset the flag as needed. However, it seems that if I catch the KeyDown, the KeyUp event is not fired, and/or vice-versa (and let's not even talk about the KeyPressed event !). In short, I have
private void myApp_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyDown"); e.Handled=false; } private void myApp_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyUp"); }
and I see only "KeyDown" message boxes. Note that the line e.handled=false; can be there or not, but the behavior seems to be always the same. What can I do? Do I have to dive intopublic class TestMessageFilter: IMessageFilter {...}
andpublic bool HandleKeys(Keys keyCode) { bool ret = true; switch(keyCode) { case Keys.Shift: ...do stuff... default: ret = false; break; } return ret; }
??? Thanks in advance, F.O.R.Go it ! Phew* ! I was seeing only KeyDown message boxes because of timing issues; before that, I was doing a mistake in the code of the KeyUp handler and the shiftPressed was being set to true when I was de-pressing the key. For anyone else who needs this, this seems to work:
private void myApp_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Shift) shiftPressed=true; e.Handled=false; } private void myApp_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { shiftPressed=e.Shift; }
We now return you to the universe next door. F.O.R.