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. KeyDown for arrows

KeyDown for arrows

Scheduled Pinned Locked Moved C#
question
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.
  • S Offline
    S Offline
    S O S
    wrote on last edited by
    #1

    How can I get the KeyDown event to fire for the arrow keys?

    N I 3 Replies Last reply
    0
    • S S O S

      How can I get the KeyDown event to fire for the arrow keys?

      N Offline
      N Offline
      Nnamdi Onyeyiri
      wrote on last edited by
      #2

      you have to override the IsInputKey method, so that it returns true with the key arrows.

      protected override bool IsInputKey(Keys key)
      {
      if (key.KeyCode == Keys.Up) return true;
      else if (key.KeyCode == Keys.Down) return true;
      else if (key.KeyCode == Keys.Left) return true;
      else if (key.KeyCode == Keys.Right) return true;
      else return base.IsInputKey(key);
      }

      Another Post by NnamdiOnyeyiri l Website

      S 1 Reply Last reply
      0
      • N Nnamdi Onyeyiri

        you have to override the IsInputKey method, so that it returns true with the key arrows.

        protected override bool IsInputKey(Keys key)
        {
        if (key.KeyCode == Keys.Up) return true;
        else if (key.KeyCode == Keys.Down) return true;
        else if (key.KeyCode == Keys.Left) return true;
        else if (key.KeyCode == Keys.Right) return true;
        else return base.IsInputKey(key);
        }

        Another Post by NnamdiOnyeyiri l Website

        S Offline
        S Offline
        S O S
        wrote on last edited by
        #3

        I tried that, but when I put a breakpoint there, I see the function never gets called... hmm?

        N 1 Reply Last reply
        0
        • S S O S

          I tried that, but when I put a breakpoint there, I see the function never gets called... hmm?

          N Offline
          N Offline
          Nnamdi Onyeyiri
          wrote on last edited by
          #4

          hmmm, it should do, it does when i create controls, are you creating a control from scratch? or adapting a current one? Another Post by NnamdiOnyeyiri l Website

          1 Reply Last reply
          0
          • S S O S

            How can I get the KeyDown event to fire for the arrow keys?

            I Offline
            I Offline
            Ista
            wrote on last edited by
            #5

            To override the form or custom controls protected override bool ProcessKeyPreview( ref System.Windows.Forms.Message mes ) { Keys keyCode = (Keys)(int)mes.WParam & Keys.KeyCode; if((mes.Msg == WM_KEYDOWN || mes.Msg == WM_KEYUP) && keyCode == Keys.Down ) { MessageBox.Show("Arrow handled"); return true; } else return false; } for other controls like a textbox Form1_Load() this.myControl.KeyDown += new KeyEventHandler(this.MyKeyDownEvent); ... private void MyKeyDownEvent( object sender, KeyEventArgs e ) { if ( e.KeyCode == Keys.A ) MessageBox.SHow("Hey you typed an AS"); } there you go dude the above works for override the message loop for custom controls and the below works for controls inside your container. nick I'm not an expert yet, but I play one at work. Yeah and here too.

            1 Reply Last reply
            0
            • S S O S

              How can I get the KeyDown event to fire for the arrow keys?

              I Offline
              I Offline
              Ista
              wrote on last edited by
              #6

              Downt forget the defines, I dont know where they exist in c# so i just make them consts private const int WM_KEYDOWN 0x100; private const int WM_KEYUP 0x101; your C++ include files have all the values inside the libraries. nick I'm not an expert yet, but I play one at work. Yeah and here too.

              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