Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determing the 2nd key pressed in a ALT+KEY hotkey combination 1

Status
Not open for further replies.

Fred

MIS
Sep 15, 1998
13
US
I have a form where the user can hotkey from a textbox with either an ALT+S to a &Save button or an ALT+U to an &Undo button. I'm using the KeyDown event on the textbox to try to capture which combination is pressed. I know how to check the SHIFT parameter to determine that the ALT key was pressed, but where can I determine the letter pressed?
I am checking the KeyCode, but I always get an 18, the keycode for the ALT key, whether they press ALT+S or ALT+U.

Thanks!
 
From MSDN

KeyDown, KeyUp Events Example
This example demonstrates a generic keyboard handler that responds to the F2 key and to all the associated ALT, SHIFT, and CTRL key combinations. The key constants are listed in the Visual Basic (VB) object library in the Object Browser. To try this example, paste the code into the Declarations section of a form that contains a TextBox control, and then press F5 and press F2 with various combinations of the ALT, SHIFT, and CTRL keys.

Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
Dim ShiftDown, AltDown, CtrlDown, Txt
ShiftDown = (Shift And vbShiftMask) > 0
AltDown = (Shift And vbAltMask) > 0
CtrlDown = (Shift And vbCtrlMask) > 0
If KeyCode = vbKeyF2 Then ' Display key combinations.
If ShiftDown And CtrlDown And AltDown Then
Txt = "SHIFT+CTRL+ALT+F2."
ElseIf ShiftDown And AltDown Then
Txt = "SHIFT+ALT+F2."
ElseIf ShiftDown And CtrlDown Then
Txt = "SHIFT+CTRL+F2."
ElseIf CtrlDown And AltDown Then
Txt = "CTRL+ALT+F2."
ElseIf ShiftDown Then
Txt = "SHIFT+F2."
ElseIf CtrlDown Then
Txt = "CTRL+F2."
ElseIf AltDown Then
Txt = "ALT+F2."
ElseIf SHIFT = 0 Then
Txt = "F2."
End If
Text1.Text = "You pressed " & Txt
End If
End Sub

 
here is the code for ur ALT+S and ALT+U

If AltDown And KeyCode = 83 Then
MsgBox "ALT+S"
ElseIf AltDown And KeyCode = 85 Then
MsgBox "ALT+U"
End If

with regards
webspy
 
Justin and Webspy:

Thanks for your replies, but I don't think I explained the problem correctly.

The KeyDown event handles the function keys alright, but it doesn't handle the alphabetic/ANSI keys the same way. In my scenario the KeyCode parameter is always returning a keycode of 18 (which is the ALT value). KeyCode never returns the expected keycodes for either of the S's [83(115)] or the U's [85(117)]; therefore the
Altdown AND Keycode = vbKeyU
test always returns FALSE.

I'm still stuck.
 
Event keydown has got 2 parameters KeyCode is the ascii code of the carakter pressed (allways uppercase) the Shift is the code for alt, shift, ctrl, caps lock if you press any carakter Shift would be zero.
So only when Shift = 4 and Keycode = 83 the als and s is pressed. You should allso check if the keydown got the KeyCode parameter first and the Shift comes second.
See code below.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 4 And KeyCode = 83 Then
MsgBox "alt + s"
End If
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top