Here is an example of discarding the return key with a system-wide low level keyboard hook.
Note, system-wide hooks are, in general, not a good idea because they can slow down your system if not properly used.
in a module
VB Code:
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 hHook As Long _
) As Long
Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Long, _
ByVal ncode As Long, _
ByVal wParam As Long, _
lParam As Any _
) As Long
Public Const WH_KEYBOARD_LL = 13
Private Const HC_ACTION = 0
Private Const HC_NOREMOVE = 3
Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private hHook As Long
Public IsHooked As Boolean
Public Sub SetKeyboardHook()
If IsHooked Then
MsgBox "Don't hook WH_KEYBOARD_LL twice or you will be unable to unhook it."
i want to get a lowlevel hook of another program (Counter-Strike actually) so under certain conditions keypresses get detected by my program and not allowed to pass to CS, but im not sure how to get the "hInstance" of Counter-Strike.
Is "hInstance" the same as hwnd? or is there some other identifier i need?
The admin (one of my friends) keeps reading the "team_say" messages and so he knows what we are doing, so im making a chat program that ill give to my other friends so we can talk and he cant read what we are saying.
Well, thats what i setup (because i couldnt get low-level hooking to work), but then i couldnt find anything that will block the keystrokes from being picked up by CS.
Any ideas?
Last edited by Macka007; Jul 14th, 2006 at 09:04 AM.
what do you mean you could find anything that will block the keystrokes from being picked up by CS.
I used the hook above, changed this line:
VB Code:
If lParam.vkCode = [B]vbKeyA [/B]Then
and not able to press the A key when I tried to change my name in CS:S (wasn't able to test it in a game cos it failed to load - but i think that's just my computer not being able to handle it)
Ok, it doesnt work as well as it should. I intergrated the Key Hook into my chat program.
To activate the hook you press ALT+Z, that does the following procedure.
When you press the Return key it Unhooks and the CS continues to work normally.
The first time it is activated it works fine, the second time however it allows keys to pass to CS and then CS starts getting erratic behavior and doesnt respond properly to key strokes (its hard to describe the symptoms as they seem to change each time).
Just thought of something.... i could keep the hook active but bypass the filtering and cancellation routines unless ALT+Z is pressed...
Might work, Ill keep you posted.
VB Code:
'Some other code to deal with shortcuts
If Message.wParam = &HBFF1& Then
SetKeyboardHook
frmChatInjection.Shape2.BackStyle = 0
MakeTransparent frmChatInjection
End If
'Some other code to deal with shortcuts
'***********************************************
'*********** Module to do with Hooks*****************
'***********************************************
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 hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Just thought of something.... i could keep the hook active but bypass the filtering and cancellation routines unless ALT+Z is pressed...
that was my initial thought. Try something like the below.
When the form loads you can type normally into textbox. Press Alt+T and then anything you type won't be passed to the textbox. Press Enter to see what you typed or press Esc to discard it. After pressing Return or Esc you can type in the textbox again:
VB Code:
Public Function LowLevelKeyboardProc(ByVal uCode As Long, ByVal wParam As Long, lParam As KBDLLHOOKSTRUCT) As Long
That is amazing, how did you know to do it like that?
Im guessing the .Flags parameter tells you if ctrl, alt, shift are pressed.
What is the bLog Var for?
And what is the wParam for?
i looked in the API guide for wParam and lParam but all it said was
· wParam
Specifies the wParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain.
· lParam
Specifies the lParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain.
This is the code I have been using (its got a couple of patches here and there because I was trying to get capitals and '!' through ')' to work, but now I have decided they are not as important.
To run the code you may need Microsoft Direct Text-To-Speech and a L&H TTS engine installed.
(Not bShift) * -32 and IIf(bShift, 0, 32) should have the same outcome. Does my example project work correctly for you (it does for me).
anyhow, be aware that using IIf is significantly slower than using the If Then construct, and since speed is important here - if you can't get the method i suggested to work then you should use an If...Then block.
Regarding your number coding you have to think about how many operations you're performing each time. Let's take an example - say the lParam.vkCode is for vbKey0.
your code matches on the first line after "If bShift Then", but it then still has to check for the other 9 numbers - all of those operations are wasting time. Also if there is a match then you know Cnt has an absolute value it's being assigned, yet for each one you're performing a unnecessary operation:
VB Code:
Case vbKey0 To vbKey9
If bLog Then
If wParam = WM_KEYUP Then
If bShift Then
Select Case lParam.vkCode
Case vbKey0
cnt = 41
Case vbKey1, vbKey3 To vbKey5
cnt = lParam.vkCode - 16
Case vbKey2
cnt = 64
Case vbKey6
cnt = 94
Case vbKey7, vbKey9
cnt = lParam.vkCode - 17
Case vbKey8
cnt = 42
End Select
sText = sText & Chr$(cnt)
Else
If wParam = WM_KEYUP Then sText = sText & Chr$(lParam.vkCode)
Contrary to what the script says, if you hook it twice. It only hooks it until Windows restarts, otherwise it stays hooked even after application termination.
It isn't writing any settings, it's just hooking that keystroke, thus it is only a change of a low-level setting without saving.
Also, I have a question regarding more advanced key hooking.
I'm trying to make a password application that runs through svchost.exe as a secondary to Windows logon. Requires the user to use Safe-Mode to bypass it, but using Safe-Mode will prevent any third party software from initializing which in turn will make the system more secure as I have the hidden Administrator account disabled and the guest account disabled as well.
By making a full-screened application that key hooks the following:
Ctrl + Alt + Del
Ctrl + Shift + Esc
Alt + F4
Alt + Tab
Ctrl + Esc
Alt + Esc
Left Win Key + D
Left Win Key + E
Right Win Key + D
Right Win Key + E
Left Win Key
Right Win Key
Open Context Menu Key
Alt Tab (Lock) Key (AKA Window Panes Key)
Edit:
Sorry for reviving an ancient thread, but this could be elaborated on further.
Last edited by Aleoss; Dec 6th, 2011 at 12:03 AM.
Reason: Editing reason described in post.