Preventing keystroke repeat

Hi,

I have developed a keystroke with a timer for manually counting events. The problem is, however, that if I don’t release the key fast enough, the app counts more than one event. Inserting a delay after the keystroke prevents this, except that I then cannot count faster than the delay allows.

Apparently no event is triggered by releasing the key, - or have I missed something?

Any help greatly appreciated.

Strange

How do you get the key ? Could you post the code ?

Hi Michel,

I have a Thread, and a Timer with the following Action:

me.period = 75

If Keyboard.AsyncKeyDown(123) then
//do something with the left arrow key
volumeFact = 20log(volumeStepdB)
volumeVal = volumeVal/volumeFact
Msgbox "Fact, Val = " + Str(volumeFact) + ", " + Str(volumeVal)
VolumeFactTextField.Text = Str(volumeFact)
VolumeTextField.Text = Str(volumeVal)
DelayMBS 0.5
end if
If Keyboard.AsyncKeyDown(124) then
//do something with the right arrow key…
volumeFact = 20
log(volumeStepdB)
volumeVal = volumeVal*volumeFact
VolumeFactTextField.Text = Str(volumeFact)
VolumeTextField.Text = Str(volumeVal)
DelayMBS 0.5
end if
If Keyboard.AsyncKeyDown(125) then
//do something with the down arrow key…
volumeStepdB = volumeStepdB - 0.5
VolumeStepdBTextField.Text = Str(volumeStepdB)
end if
If Keyboard.AsyncKeyDown(126) then
//do something with the Up arrow key…
volumeStepdB = volumeStepdB + 0.5
VolumeStepdBTextField.Text = Str(volumeStepdB)

end if

And an Open Method:

volumeVal = 25.
volumeStepdB = 1.
volumeFact = 20*log(volumeStepdB)
MsgBox "Init volumeFact = " + Str(volumeFact)

That’s about it.

Strange

Since you’re detecting the keystroke yourself, you must also detect the key release yourself. You must maintain a “latch” that represents the state of the key. Something like

if Keyboard.AsyncKeyDown(123) then
   if MyOwnKeyboardStatusFlag(123) then
      // key is being held down from before, do nothing
   else
      // do something with the new keystroke
       MyOwnKeyboardStatusFlag(123) = True
    end
else
   // the key is no longer down
   MyOwnKeyboardStatusFlag(123) = False
end

I suspected as much. AsyncKeyDown does not work like KeyDown. As you noted, there is no keyUp.

What you do is set a flag, for instance Keydown123 as Boolean to prevent the repeat.

Something like this should not repeat (top of my head) :

If Keyboard.AsyncKeyDown(123) and not Keydown123 then // *** Check if not already down
Keydown123 = True // *** This will prevent the repeat
//do something with the left arrow key
volumeFact = 20*log(volumeStepdB)
volumeVal = volumeVal/volumeFact
Msgbox "Fact, Val = " + Str(volumeFact) + ", " + Str(volumeVal)
VolumeFactTextField.Text = Str(volumeFact)
VolumeTextField.Text = Str(volumeVal)
DelayMBS 0.5
elseIf Keydown123 then // *** Will reset the flag when the key is released
Keydown123 = Keyboard.AsyncKeyDown(123)
end if

Dang. Tim was faster :wink:

Tim and Michel,

Thanks very much for your suggestions. I shall be working on them forthwith.

Strange

Tim and Michel,

Thanks again very much for your solutions. They both work perfectly!

Strange