?
Solved

Keyboard hooks

Posted on 2001-06-24
5
Medium Priority
?
392 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
[X]
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
  • 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

765 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