Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Trapping multiple keys

Got a problem - need to trap more than one keys eg: ctrl+enter or ctrl+enter+F1 etc. I am managing but putting in a loop and using the keyDown event. Then I need 2! in case of two key strokes and 3! in case of three key strokes. If this were to be done over all the objects like combo/text box I am using, it becomes pretty painful. Is there an alternative?? Thanx in advance
  • 2
1 Solution
i was thinking about hotkeys, but couln't find anything.
a key with a shift key (alt, ctrl, shift) can be trapped at once, but 2 keys with a shift key can't.

but i don't think i't a big problem.
let's say ctrl+enter is the standard combo, that you just have to remember if that combo has been typed, and the addidtional key can be trapped after that.

I think this will solve your problem.  Create a new project with a form.  Add the code below to the form's module.  Run it and press ctrl-enter-F1.  You should see a message box pop up.  Other virtual key constants that you may need can be found in the Windows API viewer.  


Option Explicit

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Enum keys
     VK_CONTROL = &H11
     VK_F1 = &H70
     VK_RETURN = &HD
End Enum

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim bCtrl, bF1, bEnter As Boolean
    bCtrl = IsKeyDown(VK_CONTROL)
    bF1 = IsKeyDown(VK_F1)
    bEnter = IsKeyDown(VK_RETURN)
    If bCtrl And bF1 And bEnter Then
        MsgBox "Ctrl-Enter-F1 Pressed"
    End If
End Sub

Function IsKeyDown(key As keys) As Boolean
    IsKeyDown = (GetKeyState(key) < 0)
End Function
anand_007Author Commented:
Thanx a ton for the help!
anand_007Author Commented:
The key codes can be got by doing a search on Keycode Constants in MSDN Library  (I use October 99)

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now