locked
KeyDown Repeat Delay RRS feed

  • Question

  • Just what it says...when using the KeyDown event it delays just as if a key was held down in a text application. Of course I can change this rate in Windows itself, but I'd like to change it on run-time with my program (then of course have it revert back when the program closes).

    Is there a simple code which will change the delay/repeat rate to a set value? Thanks!

    Sunday, November 19, 2006 7:33 AM

Answers

  • In .NET 2.0, that setting is now available to your program to get, but not to modify: System.Environment.KeyboardDelay.  To actually change it, you'll have to P/Invoke the SystemParametersInfo() API call with the SPI_SETKEYBOARDDELAY argument.  Yell if you need code.
    Sunday, November 19, 2006 11:28 PM

All replies

  • Hi,

    Not that i know of but you could use a timer as a delay and set the number of milliseconds or seconds delay in your code perhaps?

    Example taken from Microsoft files.

    This example tracks the time of day in one-second increments. It uses a Button, a Label, and a Timer component on a form. The Interval property is set to 1000 (equal to one second). In the Timer event, the label's caption is set to the current time. When the button is clicked, the Enabled property is set to false, stopping the timer from updating the label's caption. The code below assumes you have a form with a Button control named Button1, a Timer control named Timer1, and a Label control named Label1.

    ' Visual Basic
    Private Sub InitializeTimer()
       ' Run this procedure in an appropriate event.
       ' Set to 1 second.
       Timer1.Interval = 1000
       ' Enable timer.
       Timer1.Enabled = True
       Button1.Text = "Enabled"
    End Sub
    
    Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    ' Set the caption to the current time.
       Label1.Text = DateTime.Now
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          If Button1.Text = "Stop" Then
             Button1.Text = "Start"
             Timer1.Enabled = False
          Else
             Button1.Text = "Stop"
             Timer1.Enabled = True
          End If
    End Sub
    
    Regards,
     
    S_DS
     
    Sunday, November 19, 2006 9:32 AM
  • I know how to use timer delays, and it will use one, but that's not what I'm after. Even with a timer delay, you still have keyboard repeat delays interfering.

    The problem is, when you hold down the button, it will go once, pause, then go more. That pause is from the keyboard repeat delay for text editors to not spam a letter when a key is held down slightly longer than normal. I'd like to remove that from my program though because I have no use for that delay.

    So like I said, this delay is based off Windows settings, not the program itself (timers will not get around it). I need code to explicitly set this Windows setting temporarily in my program. There has to be a way to do it.

    There was a way to do it with VB 3, 4, 5, and 6. Suddenly in .NET no one remembers it or knows how. It's been asked on this board before but the guy who posted it got no help other than what he already knew.

    Sunday, November 19, 2006 11:08 PM
  • In .NET 2.0, that setting is now available to your program to get, but not to modify: System.Environment.KeyboardDelay.  To actually change it, you'll have to P/Invoke the SystemParametersInfo() API call with the SPI_SETKEYBOARDDELAY argument.  Yell if you need code.
    Sunday, November 19, 2006 11:28 PM
  • Wow...well I knew there had to be a way, but does it change only the applications delay or the Windows delay as a whole whilst that application is open?

    I discovered a work-around method...to set a variable with keydown then change it with keyup, and have a timer checking the variable. It gets around that delay but I'm not sure it's the most efficient way. Your way might work better but if it changes the Windows setting as a whole when the application is opened, then that would be bad.

    I'll consider this as solved since I got what I needed solved, but I'd still like to know the question I asked in the first sentence of this post. Thanks a lot guys! (Don't worry about the code unless you want).

    Sunday, November 19, 2006 11:44 PM
  • You can post the code, i need it
    Monday, October 29, 2018 7:25 AM
  • You can post the code, i need it
    Dim nDelay As UInteger = 0
    SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, nDelay, 0)
    MsgBox("Current Delay : " & nDelay, MsgBoxStyle.Information)
    nDelay = 0
    SystemParametersInfo(SPI_SETKEYBOARDDELAY, nDelay, 0, SPIF_SENDWININICHANGE)
    MsgBox("New Delay : " & nDelay, MsgBoxStyle.Information) 

    Declarations :

        Public Const SPI_GETKEYBOARDDELAY As Integer = &H16                                                                                                 
        Public Const SPI_SETKEYBOARDDELAY As Integer = &H17                                                                                              
                                                                                                                                                         
        Public Const SPIF_UPDATEINIFILE As Integer = &H1                                                                                                 
        Public Const SPIF_SENDWININICHANGE As Integer = &H2                                                                                              
                                                                                                                                                         
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>                                                                          
        Public Shared Function SystemParametersInfo(uiAction As UInteger, uiParam As UInteger, ByRef pvParam As UInteger, fWinIni As UInteger) As Boolean
        End Function  


    Monday, October 29, 2018 8:52 AM