Solved

Keyboard Callback

Posted on 1999-01-11
4
722 Views
Last Modified: 2012-08-13
Does anyone have an example for the SetWindowsHookEx for the keyboard callback? I keep trying but the API returns 0 each time. I need to be able to trap any keys pressed in Windows.... Thanks.
0
Comment
Question by:ohayden
  • 2
4 Comments
 
LVL 2

Expert Comment

by:polygon
Comment Utility
Maybe you mistake some parameters.
Anyway, what I wanted to tell you is that monitoring all keystrokes is a bit complex with VB. Windows needs a global module for the callback and VB cannot supply such. The only solution is using a DLL, as Dlls are global modules.
You'll have to write a Dll callback function to receive the keystroke messages
0
 
LVL 2

Expert Comment

by:polygon
Comment Utility
Maybe you mistake some parameters.
Anyway, what I wanted to tell you is that monitoring all keystrokes is a bit complex with VB. Windows needs a global module for the callback and VB cannot supply such. The only solution is using a DLL, as Dlls are global modules.
You'll have to write a Dll callback function to receive the keystroke messages
0
 
LVL 3

Expert Comment

by:covington
Comment Utility
There's a PAQ that explains some of the basics, with code examples.

http://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10100692

Before you go visit it and spend the 30 points, I'll warn you that you have to check for specific keys. Unlike VB events, where a key is pressed and you are sent info on what key was pressed, it's more like you say "Was <x> pressed?" as the system returns yes or no.

I rewrote the code in the PAQ into a cleaner version. Let me know if you'd like to see it.
0
 
LVL 3

Accepted Solution

by:
groone earned 100 total points
Comment Utility
This little snippet shows how you can monitor keys across the board without the need of a fancy hook.  I only did the print screen key there are more keys and you just need to declare them in the bas module according to there numeric value.  I went ahead and listed all the keycode constants for you.  Because of length I did not list all the key syncs....I'm sure you can figure it out.

    'Paste in a form
    '==================
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

    Private Sub Form_Load()
         SetKeyboardState kbArray
         kbArray.kbByte(vk_snapshot) = 0
    End Sub

    Private Sub Timer1_Timer()
         If (GetAsyncKeyState(vk_snapshot)) < 0 Then Text1.Text = Text1 & "PrintScreen Pressed" & vbCrLf
         SetKeyboardState kbArray
         kbArray.kbByte(vk_snapshot) = 0
    End Sub

    'Paste in a Bas module
    '==================
    Option Explicit

    Public Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
    Public Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
    Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Public Type KeyboardBytes
         kbByte(0 To 255) As Byte
    End Type

    Public kbArray As KeyboardBytes
    Public Const vk_snapshot = &H2C
    Public Const vk_alt = &H12
    Public Const vk_backspace = &H8
    Public Const vk_tab = &H9
    Public Const vk_clear = &HC
    Public Const vk_enter = &HD
    Public Const vk_Shift = &H10
    Public Const vk_ctrl = &H11
    Public Const vk_pause = &H13
    Public Const vk_capslock = &H14
    Public Const vk_esc = &H1B
    Public Const vk_space = &H20
    Public Const vk_pgup = &H21
    Public Const vk_pgdwn = &H22
    Public Const vk_end = &H23
    Public Const vk_home = &H24
    Public Const vk_lftarrow = &H25
    Public Const vk_Uparrow = &H26
    Public Const vk_rghtarrow = &H27
    Public Const vk_dwnarrow = &H28
    Public Const vk_printscrn = &H2A
    Public Const vk_insert = &H2D
    Public Const vk_del = &H2E
    Public Const vk_numlock = &H90

    Public Const vk_A = 65
    Public Const vk_B = 66
    Public Const vk_C = 67
    Public Const vk_D = 68
    Public Const vk_E = 69
    Public Const vk_F = 70
    Public Const vk_G = 71
    Public Const vk_H = 72
    Public Const vk_I = 73
    Public Const vk_J = 74
    Public Const vk_K = 75
    Public Const vk_L = 76
    Public Const vk_M = 77
    Public Const vk_N = 78
    Public Const vk_O = 79
    Public Const vk_P = 80
    Public Const vk_Q = 81
    Public Const vk_R = 82
    Public Const vk_S = 83
    Public Const vk_T = 84
    Public Const vk_U = 85
    Public Const vk_V = 86
    Public Const vk_W = 87
    Public Const vk_X = 88
    Public Const vk_Y = 89
    Public Const vk_Z = 90

    Public Const vk_0 = 48
    Public Const vk_1 = 49
    Public Const vk_2 = 50
    Public Const vk_3 = 51
    Public Const vk_4 = 52
    Public Const vk_5 = 53
    Public Const vk_6 = 54
    Public Const vk_7 = 55
    Public Const vk_8 = 56
    Public Const vk_9 = 57

    Public Const vk_Numpad0 = &H60
    Public Const vk_Numpad1 = &H61
    Public Const vk_Numpad2 = &H62
    Public Const vk_Numpad3 = &H63
    Public Const vk_Numpad4 = &H64
    Public Const vk_Numpad5 = &H65
    Public Const vk_Numpad6 = &H66
    Public Const vk_Numpad7 = &H67
    Public Const vk_Numpad8 = &H68
    Public Const vk_Numpad9 = &H69
    Public Const vk_Multiply = &H6A '    MULTIPLICATION SIGN (*) key
    Public Const vk_Add = &H6B   '   PLUS SIGN (+) key
    Public Const vk_NumpadEnter = &H6C '    ENTER (keypad) key
    Public Const vk_Subtract = &H6D  '    MINUS SIGN (-) key
    Public Const vk_Decimal = &H6E     '   DECIMAL POINT(.) key
    Public Const vk_Divide = &H6F '    DIVISION SIGN (/) key

    Public Const vk_F1 = &H70 '    F1 key
    Public Const vk_F2 = &H71 '    F2 key
    Public Const vk_F3 = &H72 '    F3 key
    Public Const vk_F4 = &H73 '    F4 key
    Public Const vk_F5 = &H74 '    F5 key
    Public Const vk_F6 = &H75 '    F6 key
    Public Const vk_F7 = &H76 '    F7 key
    Public Const vk_F8 = &H77 '    F8 key
    Public Const vk_F9 = &H78 '    F9 key
    Public Const vk_F10 = &H79  '    F10 key
    Public Const vk_F11 = &H7A  '    F11 key
    Public Const vk_F12 = &H7B  '    F12 key
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now