How can I check if a key has been pressed, and check if the application is focused? :confused:
Specifically, a key combination: Ctrl+Space.
Thanks!
:wave:
Printable View
How can I check if a key has been pressed, and check if the application is focused? :confused:
Specifically, a key combination: Ctrl+Space.
Thanks!
:wave:
KeyDown requires an object to have the focus before it will respond. In this case I have used the Form.Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift And KeyCode = 32 Then Debug.Print "Shift Space"
End Sub
J.A. Coutts
Thank you so much Coutts! This really helped! Sorry for the late response, haven't been checking the thread lately
Code:Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift And vbCtrlMask And KeyCode = 32 Then Debug.Print "Ctrl+Space"
End Sub
Private Sub Form_Load()
KeyPreview = True
End Sub
That will print "Ctrl+Space" for any combination of Ctrl-Alt-Shift + Space not just Ctrl-Space...
Yes, better:
Code:If Shift = vbCtrlMask And KeyCode = 32 Then Debug.Print "Ctrl+Space"
I'm using the following routines so it really checks if the full combination is pressed and not just partially
UsageCode:
'##############################################################################
'##
'## Function fn_KeyCombination
'##
'##############################################################################
'For use with KeyDown/KeyUp Events which pass KeyCode/Shift (see fn_ShiftCombination if you only want to check Shift)
'Returns if ONLY the specific KeyCombination is pressed
'In: to check for 'any key' use eKeyCodeCheck = -1, for 'no key' use eKeyCodeCheck = 0)
'So if Alt = true, but the Ctrl and Shift not, then only return true if Alt is pressed and Ctrl/Shift not
Public Function fn_KeyCombination(ByVal KeyCode As Integer, _
ByVal Shift As Integer, _
ByVal eKeyCodeCheck As KeyCodeConstants, _
Optional ByVal bAlt As Boolean = False, _
Optional ByVal bCtrl As Boolean = False, _
Optional ByVal bShift As Boolean = False) As Boolean
fn_KeyCombination = (IIf(eKeyCodeCheck = -1, KeyCode > 0, KeyCode = eKeyCodeCheck) And _
fn_ShiftCombination(Shift, _
bAlt:=bAlt, _
bCtrl:=bCtrl, _
bShift:=bShift))
End Function
'##############################################################################
'##
'## Function fn_ShiftCombination
'##
'##############################################################################
'For use with KeyDown/KeyUp Events which pass Shift (see also fn_KeyCombination if KeyCode should also be checked)
'Returns if ONLY the specific ShiftCombination is pressed
'So if Alt = true, but the Ctrl and Shift not, then only return true if Alt is pressed and Ctrl/Shift not
Public Function fn_ShiftCombination(ByVal Shift As Integer, _
Optional ByVal bAlt As Boolean = False, _
Optional ByVal bCtrl As Boolean = False, _
Optional ByVal bShift As Boolean = False) As Boolean
fn_ShiftCombination = ((bAlt = ((Shift And vbAltMask) = vbAltMask)) And _
(bCtrl = ((Shift And vbCtrlMask) = vbCtrlMask)) And _
(bShift = ((Shift And vbShiftMask) = vbShiftMask)))
End Function
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'--------------------------------------------------------------
' <Ctrl><F1>
If fn_KeyCombination(KeyCode, Shift, vbKeyF1, bCtrl:=True) Then
'Do something
KeyCode = 0
End If
'--------------------------------------------------------------
' <Ctrl><Alt><F1>
If fn_KeyCombination(KeyCode, Shift, vbKeyF1, bAlt:=True, bCtrl:=True) Then
'Do something
KeyCode = 0
End If
'--------------------------------------------------------------
'<F5>
If fn_KeyCombination(KeyCode, Shift, vbKeyF5) Then
'Do Something
KeyCode = 0
End If '<F5>
End Sub