Skip to main content

onKeyPressed question

4 replies [Last post]
kram
Offline
Joined: 2007-10-23
Points: 0

I have a node that uses the onKeyPressed event, and Im trying to make an acceleration based animation work, however, I need it to be a smooth animation. The onKeyPressed event works, but if I hold the key down, there is a slight delay between the first press and when the "holding down" functionality kicks in, I need it to be continuous from the moment the user presses the key.

How can I get around this?

P.S. obviously I dont want the user to have to change their keyboard settings, so is there a way to implement a type of illusion to give this effect?

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexsch
Offline
Joined: 2007-07-12
Points: 0

Just start the acceleration after [i]onKeyPressed [/i]event and stop after [i]onKeyReleased [/i]event.

kram
Offline
Joined: 2007-10-23
Points: 0

Yeah, its not the starting and stopping thats my issue, its the holding down of the UP arrow. I need the acceleration of the object to increment smoothly, so the onKeyPressed event does increment it by a fixed amount, but not smoothly as there is a gap between the first press of the key and when the continuous event firing happens.

Lots of games get around this easily, but I dont know how to using JavaFX specifically. And maybe im thinking about this wrong, maybe I should create a Timeline object that can start when the user presses the UP arrow, which will increment the acceleration, then when the user releases the UP arrow, it stops the Timeline...

alexsch
Offline
Joined: 2007-07-12
Points: 0

Here is a simple example:

[code]
import javafx.application.*;

import javafx.scene.paint.*;
import javafx.scene.geometry.*;

import javafx.input.*;
import javafx.scene.*;
import javafx.scene.text.*;
import javafx.animation.*;

var flag = false;

var acceleration = 0.0;

Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames : [
KeyFrame {
time : 100ms
action: function(){
if(flag) {acceleration++;}
}
}
]
}.start();

Frame {
title: "MyApplication"
width: 250
height: 200
visible: true

stage: Stage {
content: [
Text {
fill: Color.BLUE
x: 10, y: 30
font: Font { size: 24 style: FontStyle.PLAIN }
content: bind "Acceleration: {acceleration}"
onKeyPressed: function( e: KeyEvent ):Void {
flag = true;
}
onKeyReleased: function( e: KeyEvent ):Void {
flag = false;
}
}
]
}
}[/code]

kram
Offline
Joined: 2007-10-23
Points: 0

Thanks Alex, thats pretty much the exact same code as i implemented!