Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How can I capture a 3 key press combination

Posted on 2004-08-01
7
1,312 Views
Last Modified: 2012-05-05
I need to be able to capture Ctl-Alt-F10 for an app that i am writing. now i can get Ctl-Alt, but not the F10 Here is my code:

Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim fEatKeystroke As Boolean

    If (nCode = HC_ACTION) Then
        If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
            CopyMemory p, ByVal lParam, Len(p)
           
            If (VK_F10) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0) And ((p.flags And LLKHF_ALTDOWN) <> 0) Then
                    <Do Stuff>
                    fEatKeystroke = False
                End If
            Else
                fEatKeystroke = _
                ((p.vkCode = VK_TAB) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
                ((p.vkCode = VK_ESCAPE) And ((p.flags And LLKHF_ALTDOWN) <> 0)) Or _
                ((p.vkCode = VK_ESCAPE) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0)) Or _
                (p.vkCode = VK_RWIN) Or _
                (p.vkCode = VK_LWIN) Or _
                (p.vkCode = LLKHF_ALTDOWN) Or _
                (p.vkCode = VK_ESCAPE) Or _
                (p.vkCode = VK_CONTROL And GetKeyState(VK_CONTROL))
            End If
        End If
    End If

   
    If fEatKeystroke Then
        LowLevelKeyboardProc = -1
    Else
        LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
    End If
End Function


The line "If (VK_F10) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0) And ((p.flags And LLKHF_ALTDOWN) <> 0) Then" does not work properly all the time.

Can anyone help?
0
Comment
Question by:Silvertree
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11690377
>           If (VK_F10) And ((GetKeyState(VK_CONTROL) And &H8000) <> 0) And ((p.flags And LLKHF_ALTDOWN) <> 0) Then

I think you have to start evaluating:

If (GetKeyState(VK_F10) .... ETCETERA
0
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 250 total points
ID: 11690528
Try the below. Of course, F10 has to be pressed last.


Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim kbdllhs As KBDLLHOOKSTRUCT
    If nCode = HC_ACTION Then
        If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
            Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
            'If F10...
            If kbdllhs.vkCode = vbKeyF10 Then
                'If Control...
                If (GetAsyncKeyState(vbKeyControl) And &H8000) <> 0 Then
                    'If Alt...
                    If (GetAsyncKeyState(vbKeyMenu) And &H8000) <> 0 Then
                        MsgBox "Ctrl+Alt+F10"
                    End If
                End If
            End If
        End If
    End If
    LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, nCode, wParam, lParam)
End Function

0
 
LVL 18

Expert Comment

by:JR2003
ID: 11702571
This works:

Start a new project with a blank form and add a timer control to the form
Then paste the following code into the form:

Option Explicit
Private iShiftState As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal iKey As Long) As Integer

Private Function IsKeyDown(Key As VBRUN.KeyCodeConstants) As Boolean
    If GetAsyncKeyState(Key) = -32767 Then
        IsKeyDown = True
    End If
End Function

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    iShiftState = Shift
End Sub

Private Sub Form_Load()
   KeyPreview = True
   Timer1.Interval = 100
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    iShiftState = Shift
End Sub

Private Sub Timer1_Timer()
    If (iShiftState And vbCtrlMask) <> 0 Then
        If (iShiftState And vbAltMask) <> 0 Then
            If IsKeyDown(vbKeyF10) Then
                MsgBox "Ctrl+Alt+F10 Pressed"
            End If
       End If
    End If
End Sub
0
 
LVL 12

Assisted Solution

by:BobLamberson
BobLamberson earned 250 total points
ID: 11707371
If just want to add code to each form's KeyUp event,

If KeyCode = vbKeyF10 And Shift = 6 Then
   Me.Print "Ctl-Alt-F10 keys pressed"
End If

will get the proper response.



Bob
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 12331173
ping
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 72
MS Access Search and Replace Using VBA 6 80
Adding to a VBA? 6 77
Help me. 3 60
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question