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. Capture Global Key Pressed

Capture Global Key Pressed

Scheduled Pinned Locked Moved C#
csharpjavacomtoolsquestion
5 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.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, Is there a way I can capture the keys that are being pressed without making use of an EventHandler. Maybe some old schoold C method or something? :confused: Many thanks in advance. Kind regards,

    The only programmers that are better C# programmers, are those who look like this -> :bob:

    :java: Programm3r My Blog: ^_^

    L 1 Reply Last reply
    0
    • P Programm3r

      Hi all, Is there a way I can capture the keys that are being pressed without making use of an EventHandler. Maybe some old schoold C method or something? :confused: Many thanks in advance. Kind regards,

      The only programmers that are better C# programmers, are those who look like this -> :bob:

      :java: Programm3r My Blog: ^_^

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

      Try using Google for a 'low level global keyboard hook', some articles exist on CodeProject as well, so you can even begin here. I totally didn't understand the part about not making use of an EventHandler.

      Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

      P 1 Reply Last reply
      0
      • L Lost User

        Try using Google for a 'low level global keyboard hook', some articles exist on CodeProject as well, so you can even begin here. I totally didn't understand the part about not making use of an EventHandler.

        Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        EliottA wrote:

        I totally didn't understand the part about not making use of an EventHandler

        I was referring to something like KeyPressEventHandler... and only making use of a method to get the last key pressed. For example: public Form1() { // Create a TextBox control. TextBox tb = new TextBox(); this.Controls.Add(tb); tb.KeyPress += new KeyPressEventHandler(keypressed); } private void keypressed(Object o, KeyPressEventArgs e) { // The keypressed method uses the KeyChar property to check // whether the ENTER key is pressed. // If the ENTER key is pressed, the Handled property is set to true, // to indicate the event is handled. if (e.KeyChar == (char)Keys.Return) { e.Handled = true; } }

        Thanks for the info. Kind regards,

        The only programmers that are better C# programmers, are those who look like this -> :bob:

        :java: Programm3r My Blog: ^_^

        L 1 Reply Last reply
        0
        • P Programm3r

          EliottA wrote:

          I totally didn't understand the part about not making use of an EventHandler

          I was referring to something like KeyPressEventHandler... and only making use of a method to get the last key pressed. For example: public Form1() { // Create a TextBox control. TextBox tb = new TextBox(); this.Controls.Add(tb); tb.KeyPress += new KeyPressEventHandler(keypressed); } private void keypressed(Object o, KeyPressEventArgs e) { // The keypressed method uses the KeyChar property to check // whether the ENTER key is pressed. // If the ENTER key is pressed, the Handled property is set to true, // to indicate the event is handled. if (e.KeyChar == (char)Keys.Return) { e.Handled = true; } }

          Thanks for the info. Kind regards,

          The only programmers that are better C# programmers, are those who look like this -> :bob:

          :java: Programm3r My Blog: ^_^

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

          Well the keypress is an event (as is keydown and keyup) so you would need an event handler to handle the event. :-D As for a global keyboard hook, see my above advice as to how to handle those events. :cool: PS instead of casting to a char you could test the condition by doing

          if (e.Keycode == Keys.Enter)
          e.Handled = true;

          check out the Keys Enumeration[^]

          Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

          P 1 Reply Last reply
          0
          • L Lost User

            Well the keypress is an event (as is keydown and keyup) so you would need an event handler to handle the event. :-D As for a global keyboard hook, see my above advice as to how to handle those events. :cool: PS instead of casting to a char you could test the condition by doing

            if (e.Keycode == Keys.Enter)
            e.Handled = true;

            check out the Keys Enumeration[^]

            Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

            P Offline
            P Offline
            Programm3r
            wrote on last edited by
            #5

            Thanks for the information. I found this[^], which performs a keyboard hook. Kind regards,

            The only programmers that are better C# programmers, are those who look like this -> :bob:

            :java: Programm3r My Blog: ^_^

            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