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. Catch Windows KeyPress Events?

Catch Windows KeyPress Events?

Scheduled Pinned Locked Moved C#
csharpwinformsquestion
6 Posts 3 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.
  • V Offline
    V Offline
    Verdant123
    wrote on last edited by
    #1

    is there anyway to get my C# winforms app to handle a keyboard keypress event in windows, whether or not the form is the active window? i want to be able to detect caps lock, num lock and scroll lock presses without out polling the keyboard state

    L H 2 Replies Last reply
    0
    • V Verdant123

      is there anyway to get my C# winforms app to handle a keyboard keypress event in windows, whether or not the form is the active window? i want to be able to detect caps lock, num lock and scroll lock presses without out polling the keyboard state

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      You would not be able to do this at any form level. I would imagine you'd have to write a service and have it hook into the OS (if NT even allows that type of hook) ___________________ Forgoing antagonism and separation, one enters into the harmonious oneness of all things. Lao Tzu

      1 Reply Last reply
      0
      • V Verdant123

        is there anyway to get my C# winforms app to handle a keyboard keypress event in windows, whether or not the form is the active window? i want to be able to detect caps lock, num lock and scroll lock presses without out polling the keyboard state

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

        There are basically three types of hooks you can use. One is that you override WndProc and handle all messages sent to your form whether its active or not. In the case of keyboard events, your application must be active to recieve them (except when using the other two hooks to be mentioned...). You can use an IMessageFilter along with Application.AddMessageFilter and Application.RemoveMessageFilter to add a message filter that applies to all windows (that includes controls) in your application so that you can handle keyboard events (or any other events) no matter which form is active. Finally, there's system hooks. These should be used as little as often as they hook every message of a certain type in the OS. Inefficient or buggy code can degrade performance of the OS and all applications seriously, or even cause crashes if you don't handle errors correctly. You can find more information about using system hooks in C# by reading Using Hooks from C#[^]. In any case, you'll be using Windows messaging and catching notification messages like NM_KEYDOWN, which you can find the constants for in winuser.h in the Platform SDK. Experience with Windows messaging will be helpful.

        Microsoft MVP, Visual C# My Articles

        V 1 Reply Last reply
        0
        • H Heath Stewart

          There are basically three types of hooks you can use. One is that you override WndProc and handle all messages sent to your form whether its active or not. In the case of keyboard events, your application must be active to recieve them (except when using the other two hooks to be mentioned...). You can use an IMessageFilter along with Application.AddMessageFilter and Application.RemoveMessageFilter to add a message filter that applies to all windows (that includes controls) in your application so that you can handle keyboard events (or any other events) no matter which form is active. Finally, there's system hooks. These should be used as little as often as they hook every message of a certain type in the OS. Inefficient or buggy code can degrade performance of the OS and all applications seriously, or even cause crashes if you don't handle errors correctly. You can find more information about using system hooks in C# by reading Using Hooks from C#[^]. In any case, you'll be using Windows messaging and catching notification messages like NM_KEYDOWN, which you can find the constants for in winuser.h in the Platform SDK. Experience with Windows messaging will be helpful.

          Microsoft MVP, Visual C# My Articles

          V Offline
          V Offline
          Verdant123
          wrote on last edited by
          #4

          many thanks i think i will try the IMessageFilter interface method. for now i guess i will leave it polling every few seconds

          H 1 Reply Last reply
          0
          • V Verdant123

            many thanks i think i will try the IMessageFilter interface method. for now i guess i will leave it polling every few seconds

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

            Verdant123 wrote: for now i guess i will leave it polling every few seconds :confused: Why are you polling? Every message posted to the message queue for your application is filtered through the IMessageFilter implementation you added to your application using Application.AddMessageFilter. There's no polling necessary. For example, lets say I want to write a message to the console every time a key is pressed anywhere in my application:

            public class KeyboardFilter : IMessageFilter
            {
            public bool PreFilterMessage(ref Message m)
            {
            if (m.Msg == 0x0100) // WM_KEYDOWN
            Console.WriteLine("Key down: " + m.WParam.ToString());
            return false;
            }
            }

            Microsoft MVP, Visual C# My Articles

            V 1 Reply Last reply
            0
            • H Heath Stewart

              Verdant123 wrote: for now i guess i will leave it polling every few seconds :confused: Why are you polling? Every message posted to the message queue for your application is filtered through the IMessageFilter implementation you added to your application using Application.AddMessageFilter. There's no polling necessary. For example, lets say I want to write a message to the console every time a key is pressed anywhere in my application:

              public class KeyboardFilter : IMessageFilter
              {
              public bool PreFilterMessage(ref Message m)
              {
              if (m.Msg == 0x0100) // WM_KEYDOWN
              Console.WriteLine("Key down: " + m.WParam.ToString());
              return false;
              }
              }

              Microsoft MVP, Visual C# My Articles

              V Offline
              V Offline
              Verdant123
              wrote on last edited by
              #6

              after re-reading the thread i guess i need a system hook... the application need to catch changes to the cap/num/scroll locks from anywhere within windows currently i poll the keyboard state every few seconds... to verify their values

              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