Sample of global keyboard hook
Exe
Dll
[This message has been edited by Semen Matusovski (edited April 27, 2005).]
Exe
Code:
#Compile Exe #Dim All #Register None #Include "Win32Api.Inc" %IDC_LABEL = 1000 %IDC_LINE = 1100 $UniqueName = "My Hook" Declare Function SetHook Lib "HookD.Dll" Alias "SetHook" (hWnd As Long) As Long CallBack Function DlgProc Dim Ansi As Local Byte Dim hHook As Local Dword Dim Switches As Local Dword Dim szText As Local String Select Case CbMsg Case %WM_INITDIALOG Control Add Label, CbHndl, %IDC_LABEL + 1, "Type something in external program", 5, 3, 130, 10, %WS_CHILD Or %WS_VISIBLE Or %WS_BORDER Or %SS_CENTER, %WS_EX_LEFT Or %WS_EX_LTRREADING Control Add Label, CbHndl, %IDC_LABEL + 2, "Ansi", 5, 20, 60, 10, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT, 0 Control Add Label, CbHndl, %IDC_LABEL + 3, "", 75, 19, 60, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_CENTER, %WS_EX_CLIENTEDGE Control Set Color CbHndl, %IDC_LABEL + 3, %Blue, %White Control Add Label, CbHndl, %IDC_LABEL + 4, "VirtualKeyCode", 5, 35, 60, 10, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT, 0 Control Add Label, CbHndl, %IDC_LABEL + 5, "", 75, 34, 60, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_CENTER, %WS_EX_CLIENTEDGE Control Set Color CbHndl, %IDC_LABEL + 5, %Blue, %White Control Add Line, CbHndl, %IDC_LINE + 1, "", 2, 50, 175, 1 Control Add Label, CbHndl, %IDC_LABEL + 6, "Shift", 5, 55, 60, 10, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT, 0 Control Add Label, CbHndl, %IDC_LABEL + 7, "", 75, 54, 60, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_CENTER, %WS_EX_CLIENTEDGE Control Set Color CbHndl, %IDC_LABEL + 7, %Blue, %White Control Add Label, CbHndl, %IDC_LABEL + 8, "Alt", 5, 70, 60, 10, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT, 0 Control Add Label, CbHndl, %IDC_LABEL + 9, "", 75, 69, 60, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_CENTER, %WS_EX_CLIENTEDGE Control Set Color CbHndl, %IDC_LABEL + 9, %Blue, %White Control Add Label, CbHndl, %IDC_LABEL + 10, "Ctrl", 5, 85, 60, 10, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT, 0 Control Add Label, CbHndl, %IDC_LABEL + 11, "", 75, 84, 60, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_CENTER, %WS_EX_CLIENTEDGE Control Set Color CbHndl, %IDC_LABEL + 11, %Blue, %White SetHook CbHndl Case %WM_USER + 1 Ansi = LoByt(CbWParam) If Ansi < 32 Then szText = Hex$(Ansi, 2) + " (hex)" Else szText = Chr$(Ansi) Control Set Text CbHndl, %IDC_LABEL + 3, szText Control Set Text CbHndl, %IDC_LABEL + 5, Hex$(HiByt(LoWrd(CbWParam)), 2) + " (hex)" Switches = CbLParam If Bit(Switches, 1) Then szText = "Left" Else szText = "" If Bit(Switches, 2) Then szText = "Right" Control Set Text CbHndl, %IDC_LABEL + 7, szText If Bit(Switches, 3) Then szText = "Left" Else szText = "" If Bit(Switches, 4) Then szText = "Right" Control Set Text CbHndl, %IDC_LABEL + 9, szText If Bit(Switches, 5) Then szText = "Left" Else szText = "" If Bit(Switches, 6) Then szText = "Right" Control Set Text CbHndl, %IDC_LABEL + 11, szText Case %WM_DESTROY hHook = GetProp(CbHndl, ByVal 1) If hHook Then UnhookWindowsHookEx hHook End Select End Function Function PBMain Dim hDlg As Local Dword Dialog New 0, $UniqueName, , , 140, 100, %WS_CAPTION Or %DS_MODALFRAME Or %WS_SYSMENU, %WS_EX_TOPMOST To hDlg Dialog Show Modal hDlg Call DlgProc End Function
Code:
#Compile Dll "HookD.Dll" #Register None #Dim All #Include "Win32Api.Inc" $UniqueName = "My Hook" Global hInstDLL As Long, MainDll As Long Function LibMain(ByVal hInstance As Dword, ByVal fwdReason As Dword, ByVal lpvReserved As Dword) Export As Long If fwdReason = %DLL_PROCESS_ATTACH Then hInstDLL = hInstance LibMain = 1 End Function Function HookProc(ByVal nCode As Long, ByVal wParam As Dword, ByVal lParam As Long) Export As Long Dim Ansi As Local Word Dim ControlButton As Static Byte Dim hDlg As Static Dword Dim hHook As Static Dword Dim KeyState(255) As Local Byte Dim MenuButton As Static Byte Dim ShiftButton As Static Byte Dim ScanCode As Local Byte Dim Switches As Local Dword Dim VirtualKeyCode As Local Byte Dim Vk1 As Local Byte Dim Vk2 As Local Byte hDlg = FindWindow("", $UniqueName) If hHook = 0 Then If hDlg Then hHook = GetProp(hDlg, ByVal 1) If hHook Then Function = CallNextHookEx(ByVal hHook, ByVal nCode, ByVal wParam, ByVal lParam) If IsFalse(MainDll) And (IsFalse(hDlg) Or IsFalse(hHook)) Then FreeLibrary hInstDll Else GetKeyBoardState KeyState(0) VirtualKeyCode = LoByt(wParam) ScanCode = LoByt(HiWrd(lParam)) If VirtualKeyCode = %VK_SHIFT Then If ScanCode = &H36 Then ShiftButton = 1 Else ShiftButton = 0 ElseIf VirtualKeyCode = %VK_MENU Then If (lParam And &H01000000) Then MenuButton = 1 Else MenuButton = 0 ElseIf VirtualKeyCode = %VK_CONTROL Then If (lParam And &H01000000) Then ControlButton = 1 Else ControlButton = 0 End If If ShiftButton Then Vk1 = %VK_RSHIFT: Vk2 = %VK_LSHIFT Else Vk1 = %VK_LSHIFT: Vk2 = %VK_RSHIFT KeyState(Vk1) = KeyState(%VK_SHIFT) KeyState(Vk2) = 0 If MenuButton Then Vk1 = %VK_RMENU: Vk2 = %VK_LMENU Else Vk1 = %VK_LMENU: Vk2 = %VK_RMENU KeyState(Vk1) = KeyState(%VK_MENU) KeyState(Vk2) = 0 If ControlButton Then Vk1 = %VK_RCONTROL: Vk2 = %VK_LCONTROL Else Vk1 = %VK_LCONTROL: Vk2 = %VK_RCONTROL KeyState(Vk1) = KeyState(%VK_CONTROL) KeyState(Vk2) = 0 If ToAsciiEx (VirtualKeyCode, ScanCode, ByRef KeyState(0), _ ByVal VarPtr(Ansi), 0, GetKeyboardLayout(0)) <> 1 Then Ansi = 0 If (KeyState(%VK_LSHIFT) And &H80) Then Bit Set Switches, 1 If (KeyState(%VK_RSHIFT) And &H80) Then Bit Set Switches, 2 If (KeyState(%VK_LMENU) And &H80) Then Bit Set Switches, 3 If (KeyState(%VK_RMENU) And &H80) Then Bit Set Switches, 4 If (KeyState(%VK_LCONTROL) And &H80) Then Bit Set Switches, 5 If (KeyState(%VK_RCONTROL) And &H80) Then Bit Set Switches, 6 SendMessage hDlg, %WM_USER + 1, MakWrd(Ansi, VirtualKeyCode), Switches End If End Function Function SetHook Alias "SetHook" (hWnd As Long) Export As Long Local hHook As Long hHook = SetWindowsHookEx (%WH_KEYBOARD, CodePtr(HookProc), ByVal hInstDLL, ByVal 0) SetProp hWnd, ByVal 1, ByVal hHook MainDll = 1 End Function
[This message has been edited by Semen Matusovski (edited April 27, 2005).]
Comment