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 get holding key down to repeat an action

how to get holding key down to repeat an action

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

    I want to be able to hold the down arrow key to continue to scroll on a custom control that I've written. How do I check for a key being held down? Is there a property or something to let a keydown event repeat itself?

    S N B 3 Replies Last reply
    0
    • B bradsnobar

      I want to be able to hold the down arrow key to continue to scroll on a custom control that I've written. How do I check for a key being held down? Is there a property or something to let a keydown event repeat itself?

      S Offline
      S Offline
      sam
      wrote on last edited by
      #2

      implement keyDown event in your control and write your code there. and where ever you are ebedding your control.create a explict handle for keyDown event for this control and link it with keyDown event which you have created in your control sameer

      B 1 Reply Last reply
      0
      • B bradsnobar

        I want to be able to hold the down arrow key to continue to scroll on a custom control that I've written. How do I check for a key being held down? Is there a property or something to let a keydown event repeat itself?

        N Offline
        N Offline
        Nader Elshehabi
        wrote on last edited by
        #3

        Holding the key down and repeat rate is windows related. For example some users might disable this feature in their windows. Your control has nothing to do with it. All you have to do is handle the KeyDown event and implement scrolling for each stroke. When the user holds the arrow key down your program should receive multiple strokes, not just the first.

        Regards:rose:

        B 1 Reply Last reply
        0
        • S sam

          implement keyDown event in your control and write your code there. and where ever you are ebedding your control.create a explict handle for keyDown event for this control and link it with keyDown event which you have created in your control sameer

          B Offline
          B Offline
          bradsnobar
          wrote on last edited by
          #4

          sorry, that's not correct in this case.

          1 Reply Last reply
          0
          • N Nader Elshehabi

            Holding the key down and repeat rate is windows related. For example some users might disable this feature in their windows. Your control has nothing to do with it. All you have to do is handle the KeyDown event and implement scrolling for each stroke. When the user holds the arrow key down your program should receive multiple strokes, not just the first.

            Regards:rose:

            B Offline
            B Offline
            bradsnobar
            wrote on last edited by
            #5

            sorry, that's not correct in this case.

            N 1 Reply Last reply
            0
            • B bradsnobar

              I want to be able to hold the down arrow key to continue to scroll on a custom control that I've written. How do I check for a key being held down? Is there a property or something to let a keydown event repeat itself?

              B Offline
              B Offline
              bradsnobar
              wrote on last edited by
              #6

              I found the snippet to handle an arrow key being repeated in a winform control. The ProcessCmdKey method must be overriden to trap that key and the repeat message. Normally the keypress event is the one that allows repeat keypresses by holding the key down. (This rate of repeat is controlled by the bios and the OS) The keypress event does not trap the arrow keys.     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)     {       const int WM_KEYDOWN = 256;       const int WM_CHAR = 258;       const int WM_KEYUP = 257;       const int WM_SYSKEYDOWN = 260;       const int WM_SYSKEYUP = 261;       if (FromHandle(msg.HWnd) == this)       {         switch (msg.Msg)         {           case WM_KEYDOWN:             switch (keyData)             {               case Keys.Down:                 int tag = (int)msg.LParam;                 bool repeat = (tag & (1 << 30)) != 0;                 if (repeat == true)                   System.Diagnostics.Debug.WriteLine("********************");                 if (this.SelectedIndex + 1 >= this.menuItems.Count)                   this.SelectedIndex = 0;                 else                   this.SelectedIndex++;

              D 1 Reply Last reply
              0
              • B bradsnobar

                sorry, that's not correct in this case.

                N Offline
                N Offline
                Nader Elshehabi
                wrote on last edited by
                #7

                Why is that? I just made a small test: 1- Make any control that accepts focus 2- Make a ListBox for example 3- Handle the KeyDown event in the first control and in that handler call ListBox.Items.Add() for example and see if it will be repeated or not.

                Regards:rose:

                1 Reply Last reply
                0
                • B bradsnobar

                  I found the snippet to handle an arrow key being repeated in a winform control. The ProcessCmdKey method must be overriden to trap that key and the repeat message. Normally the keypress event is the one that allows repeat keypresses by holding the key down. (This rate of repeat is controlled by the bios and the OS) The keypress event does not trap the arrow keys.     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)     {       const int WM_KEYDOWN = 256;       const int WM_CHAR = 258;       const int WM_KEYUP = 257;       const int WM_SYSKEYDOWN = 260;       const int WM_SYSKEYUP = 261;       if (FromHandle(msg.HWnd) == this)       {         switch (msg.Msg)         {           case WM_KEYDOWN:             switch (keyData)             {               case Keys.Down:                 int tag = (int)msg.LParam;                 bool repeat = (tag & (1 << 30)) != 0;                 if (repeat == true)                   System.Diagnostics.Debug.WriteLine("********************");                 if (this.SelectedIndex + 1 >= this.menuItems.Count)                   this.SelectedIndex = 0;                 else                   this.SelectedIndex++;

                  D Offline
                  D Offline
                  DiegoValdevino
                  wrote on last edited by
                  #8

                  Some quote from the MSDN[^] site: "The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events." You should do Keydown's instead of ProcessCmdKey, that's what you are doing anyways...

                  bradsnobar wrote:

                  switch (msg.Msg) { case WM_KEYDOWN:

                  Just do it on the .NET way.

                  bradsnobar wrote:

                  switch (keyData) { case Keys.Down:

                  You could write your switch statement on OnKeyDown method: private void _yourcontrol__KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch(e.KeyCode) { case Key.Down: //Do something ... } ... } Beside, embedding System const's in your code is not a nice thing to do. Microsoft could just change the values and your code will go to the outerspace.:laugh: ps.: Some words from MSDN[^]: Notes to Inheritors When overriding the ProcessCmdKey method in a derived class, a control should return true to indicate that it has processed the key. For keys that are not processed by the control, the result of calling the base class's ProcessCmdKey method should be returned. Controls will seldom, if ever, need to override this method. Diego Valdevino

                  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