Solved

Keyboard hooks

Posted on 2001-06-24
5
383 Views
Last Modified: 2010-05-02
i set a keyboard hook when my form is loaded and i'm using the lparam to highlight keys on my virtual keyboard my problem is how do i tell a difference between the left and right control or alt keys?
0
Comment
Question by:Ryan9999
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 6223307
Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
Private Const VK_LSHIFT = &HA0
Private Const VK_RSHIFT = &HA1
Private Const VK_LCONTROL = &HA2
Private Const VK_RCONTROL = &HA3
Private Const VK_LMENU = &HA4
Private Const VK_RMENU = &HA5


Cheers
0
 
LVL 1

Author Comment

by:Ryan9999
ID: 6223570
that doesn't seem to be working
0
 
LVL 27

Expert Comment

by:Ark
ID: 6223879
Hi
another way:

Private Function IsBitSet(iBitString As Long, ByVal lBitNo As Integer) As Boolean
    If lBitNo = 31 Then
        IsBitSet = iBitString < 0
    Else
        IsBitSet = iBitString And (2 ^ lBitNo)
    End If
End Function

Now in your hook callback function:
If IsBitSet(lParam,24) Then
Debug.print "Right shift or control pressed"

Cheers
0
 
LVL 1

Author Comment

by:Ryan9999
ID: 6224157
I must be doing something wrong because that isn't working either here is the code i am using in my module

Option Explicit

Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long,ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal idHook As Long) As Long

Dim hHook As Long

Public Sub HookKeyboard()
  hHook = SetWindowsHookEx(2, AddressOf Keyboard, App.hInstance, 0)
End Sub

Public Sub UnhookKeyboard()
  If hHook Then
    UnhookWindowsHookEx hHook
    hHook = 0
  End If
End Sub

Private Function Keyboard(ByVal idHook As Long, ByVal lParam As Long, _
  ByVal wParam As Long) As Long

  Form1.Caption = lParam
End Function

0
 
LVL 27

Accepted Solution

by:
Ark earned 50 total points
ID: 6225543
Corrcetions on your code:

1.    hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf Keyboard, App.hInstance, App.ThreadID)

2. Function Keyboard(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If idHook < 0 Then
        Keyboard = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    Else
       If IsBitSet(lParam,24) Then  
          Form1.Caption = wParam & " (Right)"
       Else
          Form1.Caption = wParam
       End if
    End If
End Function

Cheers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now