flat assembler
Message board for the users of flat assembler.

Index > Windows > keyboard hook

Author
Thread Post new topic Reply to topic
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 03 Jun 2004, 08:14
example on a system wide keyboard hook, allowing you to disable keys and such. msdn docs say hook procs must be in a dll, works here in the exe itself with no problems on win2k. in this example the right and left windows logo keys are disabled


Description: keyboard hook
Download
Filename: hook.zip
Filesize: 2.34 KB
Downloaded: 712 Time(s)

Post 03 Jun 2004, 08:14
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 03 Jun 2004, 11:11
Was it supposed to run in Win98? It doesn't...
Post 03 Jun 2004, 11:11
View user's profile Send private message Visit poster's website Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 03 Jun 2004, 14:34
cant test on anything but win2k, msdn says

Quote:

Function Information

Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as Unicode and ANSI versions on Windows NT, Windows 2000, Windows XP


possibly the hookproc must be in a dll on 95/98 systems. id like to make a dll, but ive no idea how to make a dll export a callback function, anyone have an example?
Post 03 Jun 2004, 14:34
View user's profile Send private message Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 03 Jun 2004, 14:36
oh i see it now..
Quote:

WH_KEYBOARD_LL
Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events. For more information, see the LowLevelKeyboardProc hook procedure.


try using WH_KEYBOARD on 95/98, tho it may not capture all the same keys
Post 03 Jun 2004, 14:36
View user's profile Send private message Reply with quote
inskipp



Joined: 23 Jun 2003
Posts: 25
Location: Poland
inskipp 03 Jun 2004, 15:10
Exported functions:

MakeHook(hwnd)
UnHook()

A hooked (witch MakeHook) function window receives WM_KEY message every time a key is depressed. wParam contains ASCII code. I wrote this a long time ago, so it may be buggy, but I think some parts may be useful.

[code]format PE GUI DLL
entry DLL_init

include '%fasminc%\win32a.inc'

WM_KEY=WM_USER+100d

section '.code' code readable executable shareable

proc DLL_init,hinstDLL,fdwReason,lpvReserved
enter
cmp [fdwReason],DLL_PROCESS_DETACH
je .cleanup
mov eax,[hinstDLL]
mov [hModule],eax
mov eax,1 ; successful initialization
.ex:
return
.cleanup:
call UnHook
jmp .ex
endp


proc MakeHook,hwnd ;returns 0 when error
enter
mov eax,[hwnd]
mov [hHookedWnd],eax
invoke SetWindowsHookEx,WH_KEYBOARD,HookProc,[hModule],0
mov [hHook],eax
return
endp

align 4
UnHook:
invoke UnhookWindowsHookEx,[hHook]
ret

proc HookProc,code,wParam,lParam

KeyState rb 256
cBufor rb 4

enter
push ebx
invoke CallNextHookEx,[hHook],[code],[wParam],[lParam]
push eax ;save return vaulue
cmp [code],HC_ACTION
jne .skip
test [lParam],80000000h ;is key being released
jnz .skip
lea ebx,[KeyState]
invoke GetKeyboardState,ebx
movzx eax,byte[lParam+2] ;scan code
lea ecx,[cBufor]
invoke ToAscii,[wParam],eax,ebx,ecx,0
test eax,eax
jz .skip
push eax ;push character count
movzx eax,[cBufor]
invoke PostMessage,[hHookedWnd],WM_KEY,eax,0
pop eax ;pop character count
cmp al,2
jz .skip
movzx eax,[cBufor+1]
invoke PostMessage,[hHookedWnd],WM_KEY,eax,0
.skip:
pop eax ;restore ruturn value
pop ebx
return
endp

section '.data' data readable writeable shareable

hModule dd ?
hHook dd ?
hHookedWnd dd ?

section '.idata' import data readable writeable

library user,'USER32.DLL'

import user,\
SetWindowsHookEx,'SetWindowsHookExA',\
UnhookWindowsHookEx,'UnhookWindowsHookEx',\
CallNextHookEx,'CallNextHookEx',\
PostMessage,'PostMessageA',\
ToAscii,'ToAscii',\
GetKeyboardState,'GetKeyboardState'

section '.edata' export data readable

export 'KEY.DLL',\ ;dll name
MakeHook,'MakeHook',\
UnHook,'UnHook'
section '.reloc' fixups data readable discardable[/code]
Post 03 Jun 2004, 15:10
View user's profile Send private message ICQ Number Reply with quote
asmdemon



Joined: 18 Jan 2004
Posts: 97
Location: Virginia Beach, VA
asmdemon 26 Jul 2004, 00:49
Quote:
msdn docs say hook procs must be in a dll

the reason for using a dll is due to it's ability to be accessed many times by many programs in a multitasking environment. Though, a dll is not required.

_________________
It is better to be on the right side of the devil than in his path.
Post 26 Jul 2004, 00:49
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 26 Jul 2004, 03:10
The reason most global hooks must be in DLLs is simple - the hooks need to execute in the context of multiple processes, and thus the OS has to inject hook DLLs into those process address spaces.

It's pretty easy to determine if a global hook needs to be put in a DLL or not - try keeping it in your app, and install the hook. If the system comes crashing down, you need to put it in a DLL Wink
Post 26 Jul 2004, 03:10
View user's profile Send private message Visit poster's website Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 26 Jul 2004, 03:26
works fine on 2k/xp in this example, no dll used
Post 26 Jul 2004, 03:26
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 26 Jul 2004, 03:46
PlatformSDK has this to say:
Quote:

This hook is called in the context of the thread that installed it.

...so it sounds like, officially too, no DLL is needed for this hook.
Post 26 Jul 2004, 03:46
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.