[Newbies] Detecting keyboard state / finding older fixes

Bert Freudenberg bert at freudenbergs.de
Wed Mar 5 18:33:43 UTC 2008


Assuming this is for a game, you would effectively prevent someone  
"hammering" on a key to do some action repeatedly. Also this depends  
on the actual repeat rate which varies widely between systems. In my  
example there is no such fixed timeout, you simply would check if the  
key is pressed already (i.e. it is in the keys set).

- Bert -

Am Mar 5, 2008 um 18:46 schrieb stan shepherd <squeak414 at free.fr>:

>
> Bert, Thanks for the reply and example.
>
> To get around the repeat key issue, I'm testing the elapsed time  
> since last
> keystroke. If it's too short, assume it's repeat key from the OS:
>
>    keyDelay :=    (anEvent timeStamp) - (lastEventTime at: noteNo) .
>    lastEventTime at: noteNo put: anEvent timeStamp.
>    (keyDelay < 250) ifTrue: [        "Transcript show: 'key repeat  
> ignored'; cr."
>                ^self].    "assume it's a key repeat'"
>
> ...Stan
>
> p.s. I'm sure there is a 'Squeakier' way to do this.
>
>
> Bert Freudenberg wrote:
>>
>> On Mar 4, 2008, at 19:19 , stan shepherd wrote:
>>
>>> handleKeyUp: anEvent
>>>    Transcript show: ' evt is down ',anEvent isKeyDown asString; cr.
>>>        "now press and hold a key"
>>>
>>> shows
>>>
>>> evt is down false
>>
>> Which is of course expected since only keyUp events are passed into
>> #handleKeyUp:, so #isKeyDown always is false.
>>
>> You need to handle both keyDown and keyUp events, and track the state
>> of pressed keys.
>>
>> Note that the keyValue in up/down events is system-specific (unlike
>> keyStroke events which have actual characters).
>>
>> Also note that this behavior also depends on your OS and VM version.
>> The Windows VM handles up/down events nicely for a long time, Mac VMs
>> since recently, Unix VMs are broken I think.
>>
>> I'm attaching a small example morph demonstrating this.
>>
>> - Bert -
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at lists.squeakfoundation.org
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/Detecting-keyboard-state---finding-older-fixes-tp15779144p15855607.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


More information about the Beginners mailing list