In vb6 how do i detect whether the shift key is being pressed? I have read something about vbShiftMask but i cant seem to implement it into my program.
Printable View
In vb6 how do i detect whether the shift key is being pressed? I have read something about vbShiftMask but i cant seem to implement it into my program.
You can use the KeyDown property of a textbox (or other controls)
VB Code:
Option Explicit Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If Shift = 1 Then Debug.Print "Shift " & Chr(KeyCode) End Sub
You can't detect it because there is no event for it.
It is used along with other keys like "a" to "z"...
for example if you press shift then "a", then you can detect that shift was pressed also.
[Edit], UPS.... nevermind, it seems that I was wrog :blush:
dglienna's code worked fine.
How would I make it detect that it is not being pressed?
Shift will be 0 if it isn't being pressed.
look at this:
when I let go of shift then "engupper" just remains hidden. Any suggestions?VB Code:
Private Sub form_KeyDown(KeyCode As Integer, shift As Integer) If shift = 1 Then engupper.Visible = True englower.Visible = False shiftcheck = False Else englower.Visible = True engupper.Visible = False End If End Sub
I think the IF block should be in ELSE and vice-versa.Quote:
Originally Posted by ajames
VB Code:
Private Sub form_KeyDown(KeyCode As Integer, shift As Integer) If shift = 1 Then englower.Visible = True engupper.Visible = False Else engupper.Visible = True englower.Visible = False shiftcheck = False End If End Sub
It still refuses to switch back when I let go of shift, however, if I press another button it switches back
I think you should use the GetAsyncKeyState API instead of form events. That's more reliable.
Pradeep :)
What are they and how do i do them?
It's an API that returns the key state of any key at that time - whether it is pressed or not. Just search for "GetAsyncKeyState" in this forum and you would get a lot of examples.
Pradeep :)
Something like...
VB Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Sub MyProcedure() If GetAsyncKeyState(vbKeyShift) Then 'Shift key is pressed Else 'shift is not pressed End If End Sub
Pradeep :)