0

Is there a way to increase the delay between one keypress event and the next for the same key?

I have an application where the user can scroll through images using the down/up key. They can press and release or they can just keep it pressed. There are times when the user would like to scroll faster when they maintain the key up/down pressed all the time. The duration between one keypress event and the next I believe is controlled by Windows, so let's say, if I open notepad and type in the letter "a" and I keep the key down, then the speed I see is based on a parameter Windows controls I believe.

Can I change that speed in runtime in my C# program? And once the user closes the app, then return to the normal speed rate.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Matimont
  • 739
  • 3
  • 14
  • 33
  • this is controlled by Windows and the keyboard driver. You could create a timer with your preferred interval and just see if the key is still pressed, but I wouldn't expect to receive a new keypress event each time. – ps2goat Jun 11 '14 at 19:47
  • duplicate or near-duplicate: http://stackoverflow.com/questions/171326/how-can-i-increase-the-key-repeat-rate-beyond-the-oss-limit – adv12 Jun 11 '14 at 19:47
  • possible duplicate of [How is keyboard auto-repeat implemented on a Windows PC?](http://stackoverflow.com/questions/876852/how-is-keyboard-auto-repeat-implemented-on-a-windows-pc) – John Nicholas Jun 11 '14 at 19:47
  • Right, so it will still be dependent on the keypress interval set by windows I think – Matimont Jun 11 '14 at 19:49
  • 2
    The key repeat delay is implemented by the keyboard controller inside the keyboard. Tinkering with it is entirely unreasonable since it affects every program on the machine. An unexpected program abort will leave it permanently out of whack. The simple workaround is to *not* rely on it. Use the KeyDown and KeyUp events and get repetition with a Timer that you enable with KeyDown, disable in KeyUp. You can tinker with the timer's Interval property as you please. – Hans Passant Jun 11 '14 at 19:53

4 Answers4

1

I would use something like this:

private int counter = 0;

//...

private void OnKeyDown(...)
{
   counter++;
   if (counter == <value>) //it is based on the speed
   {
       counter = 0;
       this.scrollDown();
   }
}

Or you could use a Timer that let you scroll down only if a period of time has passed. Just use a global bool and set it to true when the timer ticks a to false when you call the scrollDown method.

1

This is a system-wide setting - you can pull it up and READ it by looking at

            System.Environment.KeyboardDelay

More, this question has already been answered here: Keydown event - cool down

As noted, modifying the value isn't recommended and will likely involve a lot of additional permission settings. Better to take the approach that ps2goat mentioned and create either a timer or thread or Action that checks the status of a keydown and pass that to your slide show control accordingly.

Community
  • 1
  • 1
Redgum
  • 388
  • 5
  • 14
1

The easiest way to do this is to create events on your form for the KeyDown and KeyUp events, check the key being pressed / released and track the keys and how long they have been pressed, create a scale for how quickly you want to scroll and scroll the number of images.

Create a new timer on your form for the TimerTick method below

Something like this (untested, will not compile):

bool KeyIsDown = false;
int count = 0;

void OnKeyDown(sender, key e) //This Needs to match the event to call KeyDown
{
    if(e.key == KeyToScroll)
    {
        KeyIsDown = true;
        count = 0;
        e.handled = true;
    }
}
void OnKeyUp(sender, key e) //This Needs to match the event for KeyUp
{
    if(e.key = KeyToScroll)
    {
        KeyIsDown = false;
        e.handled = true;
    }
}

void TimerTick() //This needs to match the Timer_OnTick event
{
    if(KeyIsDown)
    {
        count++;
        ScrollImage()
    }
}

void ScrollImage()
{
    //TODO Scroll image based on count (how long the button has been pressed)
}
RyanTimmons91
  • 460
  • 1
  • 5
  • 17
-1

Keep a count of how many key presses were received and change picture only on a modulo of that number.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37