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. C / C++ / MFC
  4. Keybaord hooking

Keybaord hooking

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 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.
  • A Offline
    A Offline
    ashtwin
    wrote on last edited by
    #1

    Hi, i am using the function SetWindowsHookEx() to set the keyboard hook. The code snippet is as follows

    if(myHook == NULL)
    myHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)myFunction, AfxGetApp()->m_hInstance, NULL);

    LRESULT myFunction( int ncode, WPARAM wparam, LPARAM lparam)
    {
    if((wparam == VK_F10))
    {
    DoSomething();
    }
    return ( CallNextHookEx(myHook,ncode,wparam,lparam) );
    }

    The problem is that the function myFunction() is getting called two times. Can someone tell me how can i avoid this. Thanks

    L 1 Reply Last reply
    0
    • A ashtwin

      Hi, i am using the function SetWindowsHookEx() to set the keyboard hook. The code snippet is as follows

      if(myHook == NULL)
      myHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)myFunction, AfxGetApp()->m_hInstance, NULL);

      LRESULT myFunction( int ncode, WPARAM wparam, LPARAM lparam)
      {
      if((wparam == VK_F10))
      {
      DoSomething();
      }
      return ( CallNextHookEx(myHook,ncode,wparam,lparam) );
      }

      The problem is that the function myFunction() is getting called two times. Can someone tell me how can i avoid this. Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ashtwin wrote:

      The problem is that the function myFunction() is getting called two times.

      This is normal behavior. When you press a key on your keyboard it generates a keypress and release and if you hold the key down it generates repeat keys. You should probably have a look at how the the KeyboardProc [^]hook procedure is implemented. And if you read About Keyboard Input[^] you will find that you can check the transition state of the key with something like:if (HIWORD(lparam) & KF_UP) { //The key was released. }
      Best Wishes, -David Delaune

      A 1 Reply Last reply
      0
      • L Lost User

        ashtwin wrote:

        The problem is that the function myFunction() is getting called two times.

        This is normal behavior. When you press a key on your keyboard it generates a keypress and release and if you hold the key down it generates repeat keys. You should probably have a look at how the the KeyboardProc [^]hook procedure is implemented. And if you read About Keyboard Input[^] you will find that you can check the transition state of the key with something like:if (HIWORD(lparam) & KF_UP) { //The key was released. }
        Best Wishes, -David Delaune

        A Offline
        A Offline
        ashtwin
        wrote on last edited by
        #3

        Thanks is is working after checking the condition if((wparam == VK_F10) && (lparam < 0)) { DoSomething(); }

        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