09-01-2003, 09:36 AM
Hi everyone ;)
since i am lowskilled in programming i got a simple question ;)
i am trying to make a program that recognizes wether a key has been pressed and then reacts according to the pressed key or keycombination.
so what i actually want to know is which value is returned when i press strg + key or shift + key or alt + key ?
thx in advance :)
ps: if there are some words missing, sorry, because today is really bad with my typing ;)
09-01-2003, 09:45 AM
Look in the VB help for Keydown.
In keydown there is a variable called shift which can be used to indicate shift/alt/ctrl conditions.
to quote MSDN....
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."
Text1.Text = "You pressed " & Txt
but you can't catch system keypresses such as ctrl+alt+del in this way (which is a good thing)
09-01-2003, 09:51 AM
Assuming you are operating with Forms, set the Form's KeyPreview property to true - this causes the Form to capture all keyboard input regardless of which control really has the focus. Then, in the Form's KeyDown event, check the KeyCode parameter to see if it is the ASCII code of the char you want to react to. You can use the Shift parameter to check the state of the Shift, Control, and Alt keys, using the vbShiftMask, vbControlMask, and vbAltMask constants:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (Shift = vbAltMask) And (UCase$(Chr$(KeyCode)) = "G") Then
'Alt-G was pressed
End SubYou can also combine the Shift masks to allow Alt/Shift/Ctrl combinations.If Shift = (vbAltMask Or vbShiftMask) Then 'Will only fire when both Alt and Shift are pressed.
D'oh, beaten to the punch. :p
09-01-2003, 09:55 AM
D'oh, beaten to the punch. :p[/QUOTEPOST]
though being beaten, your hint with the forms flag it was as helpful as the first tip, so thx a lot to btoh of you :D
and sorry for being too lazy to check msdn ;)