Solved

Keyboard hooks

Posted on 2001-06-24
5
388 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 28

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 28

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 28

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 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