Solved

Keyboard hooks

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

772 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