Home > Archive > PerlTk > March 2005 > Keys pressed simultanously
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Keys pressed simultanously
|
|
| René Scheibe 2005-02-26, 8:56 am |
| Hi,
I am hacking a little game and surfed the web for hours
but didnt find something concerning my problem.
I use the four arrow keys for driving a car. I want to
be able to steer and brake at the same time.
But with the keybindings just the action for the last
pressed key gets executed. But want the actions of all
pressed down keys to be executed. How can this be done?
And how about the repeat rate of the keys? I mean how
can i setup for example how fast my steering goes from
left to right (changing of a variable) when a key is
pressed the whole time.
Thanks
Rene
| |
| Jack D 2005-02-26, 8:57 pm |
|
"René Scheibe" <Rene.Scheibe@stud.tu-ilmenau.de> wrote in message
news:38b7klF5lt6itU1@individual.net...
> Hi,
>
> I am hacking a little game and surfed the web for hours
> but didnt find something concerning my problem.
>
> I use the four arrow keys for driving a car. I want to
> be able to steer and brake at the same time.
> But with the keybindings just the action for the last
> pressed key gets executed. But want the actions of all
> pressed down keys to be executed.
> How can this be done?
Slaven offers a very good suggestion at:
http://groups.google.ca/groups?selm...0vran.herceg.de
Another suggestion would be to store the keys pressed in a hash on a
keypress and delete them from the hash on a keyrelease. Then you can use
this hash to determine which keys are currently pressed for your coding.
> And how about the repeat rate of the keys? I mean how
> can i setup for example how fast my steering goes from
> left to right (changing of a variable) when a key is
> pressed the whole time.
What platform? For win32:
http://groups.google.ca/groups?selm....newsranger.com
For X11 - I think you are out of luck for changing the repeat rate. Here is
a quote from X faqs:
"The base X11 protocol, doesn't provide for varying the auto-repeat rate,
which is a capability not supported by all systems."
Jack
| |
| René Scheibe 2005-03-01, 3:58 pm |
| Ok I have done it like this (found some basics in inet).
I need 4 channels (4x2keys) for controlling a vehicle
and all have to be read simultanously. I give the code
for one of the 4 key-pairs here. With this code controlling
the 4 channels at the same time now is working.
But I have a big problem. The autorepeat of the keys.
Because i dont want to change or disable it under X (so one
can use this app everywhere). I want do my own "autorepeat".
The "pressedKeysAilerons" procedure schedules itself after a
given period of time to do an autorepeat while the key is
pressed. But the problem is that changing the delay value
for the "after" procedure doesnt really alter the speed of
change as intended. The change of the variable is very jerky
(i see it because the variable is also connected to a slider)
and sometimes the values are jumping (much more than 0.01).
Can you give me some advice to optimize my code concerning
this big problem.
René
Here is the code:
proc pressedKeysAilerons {event} \
{
# event value:
# none:0, PressUp:1, ReleaseUp:2, PressDown:-1, ReleaseDown:-2
# dirAilerons values:
# none:0, up:1, down:-1
global dirAilerons
global varAilerons
if { $event == 1 && $dirAilerons == 1 } { return }
if { $event == -1 && $dirAilerons == -1 } { return }
if { $event == 1 } { set dirAilerons 1 }
if { $event == 2 } { set dirAilerons 0 }
if { $event == -1 } { set dirAilerons -1 }
if { $event == -2 } { set dirAilerons 0 }
if { $dirAilerons == 1 } {
set varAilerons [expr $varAilerons + 0.01]
after 1 { pressedKeysAilerons 0 }
}
if { $dirAilerons == -1 } {
set varAilerons [expr $varAilerons - 0.01]
after 1 { pressedKeysAilerons 0 }
}
}
set dirAilerons 0
set varAilerons 0
set procAileronsUp {}
set procAileronsDown {}
bind all <KeyPress-Right> { pressedKeysAilerons 1; after cancel
$procAileronsUp }
bind all <KeyRelease-Right> { set procAileronsUp [after 1
{pressedKeysAilerons 2}] }
bind all <KeyPress-Left> { pressedKeysAilerons -1; after cancel
$procAileronsDown }
bind all <KeyRelease-Left> { set procAileronsDown [after 1
{pressedKeysAilerons -2}] }
|
|
|
|
|