Keyup event: how to detect if another key is pressed

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I'm using vb.net/vs2003: In a combobox's keyup event I need to exit the
keyup event procedure if the user was executing (Alt + S) to save the
record, but if the user was simply entering the key (S) then I need to
finish executing the code in this sub.


I envision suto code looking like this:

IF a.Key = Keys.S AND Keys.Alt was pressed while Keys.S was pressed
then exit sub.

I could probably do something like create 2 module level variables
(FLOG_Actresses and FLOG_Key). Then when the use enters (Alt + S) in the
key down event if eke = Keys.Alt then set FLOG_Actresses to true, and the
next time the key event fires; if a.Key = Keys.S AND FLOG_Actresses then
FLOG_Key = true. Now in the keyup even regardless of which order the user
lets up the Alt and S key (Alt first or S first),

If FLOG_Key = true then
FLOG_Alt_Pressed = False
FLOG_Key = False
Exit Sub
End IF

In short, I need to know that if while the S Key was pressed, was the ALT
Key also pressed (at any time - before or during the S key being pressed),
and if so, do something.

In theory I think this would work, but I also need to do the same sort of
trapping for other key combinations such as Alt+UP, Alt+DO, Alt+NO, Alt+CO,
etc. and if my form has a bunch of combo used for data entry, there would be
a lot of extra code. I think this would be messy and un-elegant. is there
a better way to do this?




Thanks.
 
* "moondaddy said:
I'm using vb.net/vs2003: In a combobox's keyup event I need to exit the
keyup event procedure if the user was executing (Alt + S) to save the
record, but if the user was simply entering the key (S) then I need to
finish executing the code in this sub.

I envision suto code looking like this:

IF a.Key = Keys.S AND Keys.Alt was pressed while Keys.S was pressed
then exit sub.

\\\
If a.Key = Keys.S And (Control.ModifierKeys And Keys.Alt) = Keys.Alt Then
...
End If
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
Hi moondaddy,
Thanks for your post,
As you description, I understand your problem is how to know which key
combination is pressed in a KeyUpEvent with clean code.
Am I right?

If yes, you may take a look at KeyEventArgs class, it's the second param of
the KeyUpEvent handler. It already provided some boolean properties to
indicate the current status of these,
However, I think you need also check the OnKeyDown and OnKeyPressed event
if you want to prevent system beeping when you presses these hot keys. To
avoid the sound you should check them in the KeyPressed event and set
Handled to true. However, There are no info on modifier keys' state in the
KeyPressedEventArgs class, so you need save the info in KeyDown event and
clear it in KeyUp event.
The whole skeleton will become:
<code>
protected override void OnKeyDown(KeyEventArgs e)
{
//save modifier information
modifiers = e.Modifiers;
base.OnKeyDown (e);
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress (e);
if ((modifiers & Keys.Alt) == Keys.Alt && Char.ToUpper(e.KeyChar) == 'S' )
{
//prevent the sound
e.Handled = true;
}
}

protected override void OnKeyUp(KeyEventArgs e)
{
modifiers = Keys.None;
if(e.KeyCode == Keys.S && e.Alt)
{
System.Diagnostics.Debug.WriteLine("DoSomeThing");
}
base.OnKeyUp (e);
}
</code>
Does thie solve your problem?
If you have still have questions on this issue, please be free to reply to
this group.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Thanks. This doesnt seem to trap it. When I step through the code the
watch on (Control.ModifierKeys And Keys.Alt) has a value of:

Run-time exception thrown : System.ArgumentException - Invalid ObjRef
provided to {0}.

but is this were to work it seems like its what I'm looking for.

any ideas?
 
Hi moondaddy,
modifiers for KeyUp/Down events come along with the event. Take a look at
MSDN about Control.KeyUp event. and KeyEventArgs class. There is example
exacty covering your question.

So the rigth should be
if(e.KeyCode == Keys.S && e.Modifiers == Keys.Alt)
{
// Alt + S
}

HTH
B\rgds
100
 
I think i know why: The key combination is alt + S and perhaps the alt key
was let up first so when the keyup event fired for the S key, the alt key
was no longer pressed and therefore wasnt trapped via the code snippet you
provided. for the most part it seems to be working though.
 
Hi moondaddy,
if you press alt and s key seperately, windows will first set focus to the
menu then menu will receive the 'S' key to see if it is a shortcut, so your
ComboBox will not get the 'Alt+S' , The Following code snippet will make it
clear:
<code>
Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
Console.WriteLine("Alt = {0},{1}", e.Alt, e.KeyCode)
If e.KeyCode = Keys.S And e.Alt Then
e.Handled = True
MessageBox.Show("Do Something")
End If
End Sub
</code>
personally, I usually first press down the alt key and keey it down then
press the 'S' key , in this way I'll not focus to menu instead of the
hotkey.
Also I'd like to know if my work around in the previous post is helpful to
you?
If you have still have problems on this issue, please be free to reply to
this group to let me know.
Thanks

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Back
Top