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. How can I find out which key was pressed

How can I find out which key was pressed

Scheduled Pinned Locked Moved C#
question
8 Posts 4 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.
  • M Offline
    M Offline
    Mridang Agarwalla
    wrote on last edited by
    #1

    I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.

    M C H S 4 Replies Last reply
    0
    • M Mridang Agarwalla

      I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.

      M Offline
      M Offline
      Mridang Agarwalla
      wrote on last edited by
      #2

      I looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Howecer, when i do this the UI hangs. Basically, what I'm trying to accomplish here goes like this: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what?

      C 1 Reply Last reply
      0
      • M Mridang Agarwalla

        I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        If you catch the KeyPressed event, you get a different event args to KeyDown/KeyUp. There's another event for control keys as well, I think. You can also call GetAsyncKeyState via pinvoke, for full control over what control keys are down, etc, during any keyboard event, or anywhere in your code, for that matter. KeyDown/KeyUp have the properties to say if a control key was pressed, KeyPressed actually gets a different key code ( in the range 0-25, I *think* ) if Control is also down. Christian Graus - Microsoft MVP - C++

        M 1 Reply Last reply
        0
        • M Mridang Agarwalla

          I looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Howecer, when i do this the UI hangs. Basically, what I'm trying to accomplish here goes like this: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Mridang Agarwal wrote: looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Bizarre. This is a pile of bull. Why would you have to sleep to get a key event ? But good for you for trying to find the answer all the same. Mridang Agarwal wrote: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what? Oh, this is a different question ? Yes, Thread.Sleep is your answer, but it stops your app running, obviously. The other possibility is to put this code in a thread, so only that thread sleeps, or create a big old ugly loop that uses datetime.now over and over, and calls Application.DoEvents. Why do you want your app to sleep for 5 seconds ? Christian Graus - Microsoft MVP - C++

          M 1 Reply Last reply
          0
          • C Christian Graus

            Mridang Agarwal wrote: looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Bizarre. This is a pile of bull. Why would you have to sleep to get a key event ? But good for you for trying to find the answer all the same. Mridang Agarwal wrote: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what? Oh, this is a different question ? Yes, Thread.Sleep is your answer, but it stops your app running, obviously. The other possibility is to put this code in a thread, so only that thread sleeps, or create a big old ugly loop that uses datetime.now over and over, and calls Application.DoEvents. Why do you want your app to sleep for 5 seconds ? Christian Graus - Microsoft MVP - C++

            M Offline
            M Offline
            Mridang Agarwalla
            wrote on last edited by
            #5

            Oh !#$#. What he hell did I just post? That was a big goof up. I was posting a reply to another post and I accidentally posted it here. Sorry!

            1 Reply Last reply
            0
            • M Mridang Agarwalla

              I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.

              H Offline
              H Offline
              Hadi Fakhreddine
              wrote on last edited by
              #6

              You'll simply need to handle the "KeyDown" event of the TextBox like so: Just put a similar code in the KeyDown event of the TextBox: If e.KeyCode = Keys.Enter Then 'Whatever you wanna do End If The keys F1, F2, etc... are also present in the Keys enumeration (Keys.F1, Keys.Tab, Keys.a, etc...) PEACE!

              1 Reply Last reply
              0
              • M Mridang Agarwalla

                I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.

                S Offline
                S Offline
                snouto
                wrote on last edited by
                #7

                how are you my friend . listen my friend about handling the keydown event is not useful and wasting time if you try to handle it through your keydown event handling cause you will need to nest alot alot of if statements and switch keywords and it is not practical you need something like keylogger code to log any keys pressed even if your form is activated or not then you can handle the specific key when it is pressed like the following code :- /*you need first to interoperate dll found in the system itself so you need to call the namespace for handling this */ system.Runtime.InteroServices; /* Then you need to call the function of keys and override it */ [Dllimport("User32.dll")] Public static extern short GetAsyncKeyState(System.Windows.forms.Keys vkey); [Dllimport("User32.dll")] public static extern short GetAsyncKeyState(System.Int32 vkeys); /* now we called the User32.dll found in your system32 of your current windows Os and called the function that handling the keys pressed from the keyboard to get their values and iterate through system Asynchronously */ //You need to initialize timer to Listen to keys pressing //As the following System.Timers.Timer mytimer; //and initialize new keybuffer to store the keyvalue in it System.String KeyBuffer; //Initialize the event handler model of the timer to listen //to the keypressing as the following Private void mytimer_Elapsed(object o , System.Timers.ElapsedEventargs args) { foreach(System.Int32 i in Enum.GetValues(typeof(Keys))) { if(GetAsyncKeyState(i) == -32767) { //here we store the key Value in the keybuffer string //we initialized At the begining. keybuffer += Enum.GetName(typeof(keys),i); TextBox.Text += KeyBuffer; Keybuffer = ""; } } } //in the constructor add the delegate of this event public YourKeyLogger() { mytimer.Elapsed += new ElapsedEventHandler(this.mytimer_elapsed); } I hope the code can do the work for you Thanks my friend Miss With The Best And Die Like The Rest

                1 Reply Last reply
                0
                • C Christian Graus

                  If you catch the KeyPressed event, you get a different event args to KeyDown/KeyUp. There's another event for control keys as well, I think. You can also call GetAsyncKeyState via pinvoke, for full control over what control keys are down, etc, during any keyboard event, or anywhere in your code, for that matter. KeyDown/KeyUp have the properties to say if a control key was pressed, KeyPressed actually gets a different key code ( in the range 0-25, I *think* ) if Control is also down. Christian Graus - Microsoft MVP - C++

                  M Offline
                  M Offline
                  Mridang Agarwalla
                  wrote on last edited by
                  #8

                  i got the solution finally. I put the in the textbox_KeyUp event and wrote e.Keydata.ToString();

                  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