|
-
May 8th, 2026, 12:24 PM
#1
Thread Starter
New Member
Check for key press VB
How can I check if a key has been pressed, and check if the application is focused? 
Specifically, a key combination: Ctrl+Space.
Thanks!
Last edited by NoriAndris; May 8th, 2026 at 12:25 PM.
Reason: Accidently pressed Enter, sending the thread
-
May 8th, 2026, 03:25 PM
#2
Re: Check for key press VB
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift And KeyCode = 32 Then Debug.Print "Shift Space"
End Sub
KeyDown requires an object to have the focus before it will respond. In this case I have used the Form.
J.A. Coutts
-
May 10th, 2026, 03:33 PM
#3
Thread Starter
New Member
Re: Check for key press VB
Thank you so much Coutts! This really helped! Sorry for the late response, haven't been checking the thread lately
-
May 11th, 2026, 07:04 PM
#4
Re: Check for key press VB
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
-
May 11th, 2026, 08:08 PM
#5
Re: Check for key press VB
That will print "Ctrl+Space" for any combination of Ctrl-Alt-Shift + Space not just Ctrl-Space...
-
May 11th, 2026, 08:21 PM
#6
Re: Check for key press VB
Yes, better:
Code:
If Shift = vbCtrlMask And KeyCode = 32 Then Debug.Print "Ctrl+Space"
-
May 13th, 2026, 06:11 AM
#7
Hyperactive Member
Re: Check for key press VB
I'm using the following routines so it really checks if the full combination is pressed and not just partially
Code:
'##############################################################################
'##
'## 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
Usage
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|