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#
  4. Blocking Task Keys

Blocking Task Keys

Scheduled Pinned Locked Moved C#
csharpc++comjson
4 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.
  • N Offline
    N Offline
    Nagendra Kamath K
    wrote on last edited by
    #1

    Following is the VC++.NET code for the Hook Procedure passed as a parameter for the "SetWindowsHookEx" API call. This code was downloaded from http://msdn.microsoft.com/msdnmag/issues/02/09/CQA/default.aspx Can some one give the equivalent code for the following in C#. I tried a lot to do so but all in vain.

    LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
    {
    KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
    if (nCode==HC_ACTION)
    {
    BOOL bCtrlKeyDown = GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
    if ((pkh->vkCode==VK_ESCAPE && bCtrlKeyDown) ||
    (pkh->vkCode==VK_TAB && pkh->flags & LLKHF_ALTDOWN) ||
    (pkh->vkCode==VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN) ||
    (pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN))
    {
    if (g_bBeep && (wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
    MessageBeep(0);
    return 1;
    }
    }
    return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
    }

    Even a sample project which blocks all Task Keys that is Alt+Tab, Ctrl+Esc, Alt+Esc etc.. CHEERS

    H 1 Reply Last reply
    0
    • N Nagendra Kamath K

      Following is the VC++.NET code for the Hook Procedure passed as a parameter for the "SetWindowsHookEx" API call. This code was downloaded from http://msdn.microsoft.com/msdnmag/issues/02/09/CQA/default.aspx Can some one give the equivalent code for the following in C#. I tried a lot to do so but all in vain.

      LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
      {
      KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
      if (nCode==HC_ACTION)
      {
      BOOL bCtrlKeyDown = GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
      if ((pkh->vkCode==VK_ESCAPE && bCtrlKeyDown) ||
      (pkh->vkCode==VK_TAB && pkh->flags & LLKHF_ALTDOWN) ||
      (pkh->vkCode==VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN) ||
      (pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN))
      {
      if (g_bBeep && (wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
      MessageBeep(0);
      return 1;
      }
      }
      return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
      }

      Even a sample project which blocks all Task Keys that is Alt+Tab, Ctrl+Esc, Alt+Esc etc.. CHEERS

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You need to P/Invoke the necessary native APIs, such as SetWindowsHookEx. There are several articles on this on CodeProject, including Using Hooks from C#[^]. The implementation is pretty much the same, although you'll need to find the values for the various consts which are defined in winuser.h in the Platform SDK. Just define these as const int in your code.

      Microsoft MVP, Visual C# My Articles

      N 1 Reply Last reply
      0
      • H Heath Stewart

        You need to P/Invoke the necessary native APIs, such as SetWindowsHookEx. There are several articles on this on CodeProject, including Using Hooks from C#[^]. The implementation is pretty much the same, although you'll need to find the values for the various consts which are defined in winuser.h in the Platform SDK. Just define these as const int in your code.

        Microsoft MVP, Visual C# My Articles

        N Offline
        N Offline
        Nagendra Kamath K
        wrote on last edited by
        #3

        I have gone through all the articles in CodeProject reagrding hooks. I have understood quite something regarding hooks. The problems i have are: 1) What should the Delegate function should return so as to Block the keypress or not. 2) As given above, in VC++, the author has defined a structure called KBDLLHOOKSTRUCT which he is using to find which key was pressed by type casting LPARAM to this structure. I am not able to do that and hence forth not able to find which keys were pressed. CHEERS

        H 1 Reply Last reply
        0
        • N Nagendra Kamath K

          I have gone through all the articles in CodeProject reagrding hooks. I have understood quite something regarding hooks. The problems i have are: 1) What should the Delegate function should return so as to Block the keypress or not. 2) As given above, in VC++, the author has defined a structure called KBDLLHOOKSTRUCT which he is using to find which key was pressed by type casting LPARAM to this structure. I am not able to do that and hence forth not able to find which keys were pressed. CHEERS

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You don't need to cast it from the LPARAM parameter. Just include ref KBDLLHOOKSTRUCT paramName as the parameter, which automatically marshals the struct correctly. You can find more information about P/Invoke by reading Consuming Unmanaged DLL Functions[^] in the .NET Framework SDK. If you read the C# article and look at the sample source, the author would've implemented everything you need to know. See the Platform SDK for return values and other information in the documentation for the SetWindowsHookEx API and related documentation.

          Microsoft MVP, Visual C# My Articles

          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