Results 1 to 7 of 7

Thread: Check for key press VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2025
    Posts
    15

    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

  2. #2
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,668

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2025
    Posts
    15

    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

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,670

    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

  5. #5

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,670

    Re: Check for key press VB

    Yes, better:

    Code:
    If Shift = vbCtrlMask And KeyCode = 32 Then Debug.Print "Ctrl+Space"

  7. #7
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    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
  •  



Click Here to Expand Forum to Full Width