Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Set a keyboard hook

Set a keyboard hook

Scheduled Pinned Locked Moved Visual Basic
performance
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheMrProgrammer
    wrote on last edited by
    #1

    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 Structure

    Private 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")
    
    T 1 Reply Last reply
    0
    • T TheMrProgrammer

      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 Structure

      Private 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")
      
      T Offline
      T Offline
      TheMrProgrammer
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups