Set a keyboard hook
-
Hi all I am developing an app which could be called by using a hot key. i want to use a hook to do that unless there is another way of doing (without using dlls other than microsofts as i don't want to include any dlls in my app) it as i've heard that hooks degrade system performance. i use setwindowshookex,callnexthookex, and unhookwindowshookex i did a bit of googling and found microsift's eg of mouse hook and tried to convert it in a keyboard hook but without success. every time i run it the msgbox comes to say "SetWindowsHookEx Failed" here's the code:
Dim WH_KEYBOARD As Integer = 2
Shared hHook As Integer = 0
Public Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
Private hookproc As CallBack
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function SetWindowsHookEx _
(ByVal idHook As Integer, ByVal HookProc As CallBack, _
ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function CallNextHookEx _
(ByVal idHook As Integer, ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function UnhookWindowsHookEx _
(ByVal idHook As Integer) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)> Public Structure KeyHookStruct
Public pt As Point
Public hwnd As Integer
Public wHitTestCode As Integer
Public dwExtraInfo As Integer
End StructurePrivate Sub sethook()
If hHook.Equals(0) Then
hookproc = AddressOf KeyHookProc
hHook = SetWindowsHookEx(7, hookproc, IntPtr.Zero, System.Threading.Thread.CurrentThread.ManagedThreadId())
If hHook.Equals(0) Then
MsgBox("SetWindowsHookEx Failed")
Return
Else
'Button1.Text = "UnHook Windows Hook"
End If
Else
Dim ret As Boolean = UnhookWindowsHookEx(hHook)If ret.Equals(False) Then MsgBox("UnhookWindowsHookEx Failed")
-
Hi all I am developing an app which could be called by using a hot key. i want to use a hook to do that unless there is another way of doing (without using dlls other than microsofts as i don't want to include any dlls in my app) it as i've heard that hooks degrade system performance. i use setwindowshookex,callnexthookex, and unhookwindowshookex i did a bit of googling and found microsift's eg of mouse hook and tried to convert it in a keyboard hook but without success. every time i run it the msgbox comes to say "SetWindowsHookEx Failed" here's the code:
Dim WH_KEYBOARD As Integer = 2
Shared hHook As Integer = 0
Public Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
Private hookproc As CallBack
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function SetWindowsHookEx _
(ByVal idHook As Integer, ByVal HookProc As CallBack, _
ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function CallNextHookEx _
(ByVal idHook As Integer, ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function UnhookWindowsHookEx _
(ByVal idHook As Integer) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)> Public Structure KeyHookStruct
Public pt As Point
Public hwnd As Integer
Public wHitTestCode As Integer
Public dwExtraInfo As Integer
End StructurePrivate Sub sethook()
If hHook.Equals(0) Then
hookproc = AddressOf KeyHookProc
hHook = SetWindowsHookEx(7, hookproc, IntPtr.Zero, System.Threading.Thread.CurrentThread.ManagedThreadId())
If hHook.Equals(0) Then
MsgBox("SetWindowsHookEx Failed")
Return
Else
'Button1.Text = "UnHook Windows Hook"
End If
Else
Dim ret As Boolean = UnhookWindowsHookEx(hHook)If ret.Equals(False) Then MsgBox("UnhookWindowsHookEx Failed")
hey Cristian,Luc,Dave and all other genius people where are you? i need your help!!
TheMrProgrammer TheCalcMan: A no-mouse required Calculator supporting constant operator and visual effects Try it once, its awesome! Just 17.1 KB download. No installation required. No dlls. Just unrar and go. And its a freeware. http://www.hotlinkfiles.com/files/2642094\_kjwr0/TheCalcMan.rar