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 to detect arrows key in key press event handler?

How to detect arrows key in key press event handler?

Scheduled Pinned Locked Moved C#
questiontutorial
11 Posts 5 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.
  • C Offline
    C Offline
    cocoonwls
    wrote on last edited by
    #1

    dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls

    P M C D V 5 Replies Last reply
    0
    • C cocoonwls

      dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls

      P Offline
      P Offline
      Pedram Behroozi
      wrote on last edited by
      #2

      The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events. in KeyDown or KeyUp events you can detect arrows by e.KeyCode.

      When you're alone in the Dark, Fear will protect you...

      1 Reply Last reply
      0
      • C cocoonwls

        dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls

        M Offline
        M Offline
        Matjaz xyz
        wrote on last edited by
        #3

        Simple. With ASCII Code. lets take an example of pressing the keys in just form1: if(e.KeyChar==37) txtStatus.Text = "You've pressed the left arrow key."; if(e.KeyChar==38) txtStatus.Text = "You've pressed the up arrow key."; if(e.KeyChar==39) txtStatus.Text = "You've pressed the right arrow key."; This is just a small example to get you started and so that u get to know the use of ASCII char tables. Edit: Well it would be, if it would work... kinda forgot how i used this stuff. Heres a good link on explanating the keypress control: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(VS.71).aspx[^] Greets, Matjaž

        Força Barça!

        modified on Wednesday, November 5, 2008 1:54 AM

        1 Reply Last reply
        0
        • C cocoonwls

          dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls

          C Offline
          C Offline
          cocoonwls
          wrote on last edited by
          #4

          Hi Pedram Behroozi and max00slo, I have tried it before, for other char is OK, but i just can't detect the arrows key.It never fire the keydown event. What i am do in my application is, i create the keydown event handler in one of my custom UI control, and it will pass the event args to one of my class via a abstract class. any ideas are welcome. thanks in advance cocoonwls

          modified on Wednesday, November 5, 2008 2:16 AM

          P 1 Reply Last reply
          0
          • C cocoonwls

            dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            You will need to override ProcessCmdKey method to catch the arrow key press.

            HAPPY DIWALI[^]

            C 1 Reply Last reply
            0
            • C cocoonwls

              Hi Pedram Behroozi and max00slo, I have tried it before, for other char is OK, but i just can't detect the arrows key.It never fire the keydown event. What i am do in my application is, i create the keydown event handler in one of my custom UI control, and it will pass the event args to one of my class via a abstract class. any ideas are welcome. thanks in advance cocoonwls

              modified on Wednesday, November 5, 2008 2:16 AM

              P Offline
              P Offline
              Pedram Behroozi
              wrote on last edited by
              #6

              cocoonwls wrote:

              i just can't detect the arrows key

              Didn't this work?

              if (e.KeyCode == Keys.Right)
              {
              ...
              }

              When you're alone in the Dark, Fear will protect you...

              C 1 Reply Last reply
              0
              • P Pedram Behroozi

                cocoonwls wrote:

                i just can't detect the arrows key

                Didn't this work?

                if (e.KeyCode == Keys.Right)
                {
                ...
                }

                When you're alone in the Dark, Fear will protect you...

                C Offline
                C Offline
                cocoonwls
                wrote on last edited by
                #7

                Thanks for reply. yes, i also try it .keycode, but still can't fix it.. :(

                1 Reply Last reply
                0
                • D dan sh

                  You will need to override ProcessCmdKey method to catch the arrow key press.

                  HAPPY DIWALI[^]

                  C Offline
                  C Offline
                  cocoonwls
                  wrote on last edited by
                  #8

                  Hi d@nish, Can you please give me a simple explain on it, where should i put the override code, and should i add the virtual method on it(for override)? thanks in advance cocoonwls

                  D 1 Reply Last reply
                  0
                  • C cocoonwls

                    Hi d@nish, Can you please give me a simple explain on it, where should i put the override code, and should i add the virtual method on it(for override)? thanks in advance cocoonwls

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #9

                    You just need to override the method in your class. This VB.Net[^] code should get you going with it.

                    HAPPY DIWALI[^]

                    C 1 Reply Last reply
                    0
                    • D dan sh

                      You just need to override the method in your class. This VB.Net[^] code should get you going with it.

                      HAPPY DIWALI[^]

                      C Offline
                      C Offline
                      cocoonwls
                      wrote on last edited by
                      #10

                      Thanks:) i got it!

                      1 Reply Last reply
                      0
                      • C cocoonwls

                        dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls

                        V Offline
                        V Offline
                        vaynenick
                        wrote on last edited by
                        #11

                        you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().

                        Syntax:

                        C#:
                        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
                        {
                        //handle your keys here
                        }

                        Full source code..C# Arrow Key Press Vayne

                        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