• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Gixugif

I cannot get Java to properly recognize my key releases

3 posts in this topic

I am aware that there is a bug, a 12 or 13 year old bug, that exists on Linux and probably other Unix based systems, that certain ways of detecting when a key is pressed or released do not function the way one would expect. I am also aware there are a few ways around this and I have gone so far to attempt to implement this solution that can be viewed here: [url="http://stackoverflow.com/a/4679195/596359"]http://stackoverflow.../4679195/596359[/url]

The problem is that this does not seem to fix anything. The first question someone will ask I believe I can anticipate. I placed this code in a ".java" file named "RepeatingKeyEventsFixer.java". This file is in "../src/com/example/". To use it in my code I have put the following lines at the beginning of my main() method:

[code]RepeatingKeyEventsFixer repeatingkeyeventsfixer = new RepeatingKeyEventsFixer();
repeatingkeyeventsfixer.install();[/code]

[color=#000000]Still, when I press down a key, it tells me it's being pressed down and held, but it will also say it is being released with just about the same frequency. Does anyone have any idea what I may have done wrong? I have also posted this on StackExchange with poor results. I'll post the link here in case the way I phrased things there could help any more: [url="http://stackoverflow.com/q/8948238/596359"]http://stackoverflow.../8948238/596359[/url][/color]

[color=#000000]The following code is the code I am using to test if a key has been released or not. I appreciate the time any takes to consider my question.[/color]

[code]addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
processKey(e);
}
public void keyReleased(KeyEvent g){
processKeyReleased(g);
}
}
...
private void processKeyReleased(KeyEvent g){
int keyCode = g.getKeyCode();
if(!isPaused && !gameOver){
if(keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT){
System.out.println("released");
player.stopLooping();
}
}
}[/code]
0

Share this post


Link to post
Share on other sites
That's one of several issues in java.

In truth the fix is working as intended.
This is the line you need to edit in [color=#660066][size=2][left]RepeatingKeyEventsFixer.java [/left][/size][/color]to try and get the code to behave the way you want it.
[CODE]
public static final int RELEASED_LAG_MILLIS = 5;
[/CODE]

It is using a timer trick to ignore the key repeats. The way the code is written it waits for 5 milliseconds by default before it reverts back to the undesirable behavior.

There are other "bugs" that you'll eventually run into using KeyAdapters and KeyListeners. One annoying one is when multiple keys are being pressed.

These aren't really bugs in truth. They behave this way because the OS is really sending these events. It makes the events worthless to listen to but that's how it goes. In C and C++ the key repeat is turned off for games since it ruins the experience.

Oracle really should look into integrating some of the more useful features needed for games, but until then you might want to consider using the lwjgl or something similar.
With the lwjgl you can write something that looks at the keyboard state instead, and if you still needed to know if the key was just pressed or if the key was just released you could write something like this.
[CODE]
boolean keyUp = false;
private void checkInput() {
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
GameThread.isRunning = false;
}
if(Keyboard.isKeyDown(Keyboard.KEY_UP) && keyUp == false){
System.out.println("up Key Pressed");
keyUp = true;
}
if(keyUp == true && !Keyboard.isKeyDown(Keyboard.KEY_UP)){
System.out.println("up Key Released");
keyUp = false;
}
}
[/CODE]

I believe you can even turn off the repeat events if you wanted to, but basically if you want to do what you're doing in Java correctly then you'd have to implement some native code or an exiting library that already has the native code.
1

Share this post


Link to post
Share on other sites
Thank you, I see how that works now. I mean to read through the solution when I get the chance to better understand it, but it seems this solution would still be rather troubling in the context I'm using it. Since writing this game is also meant to be a learning experience I'm trying to implement many things myself that higher-level libraries would otherwise take care of such as loading sounds and graphics, but taking a looking at lwjgl just for this may be the way to go and it may prevent problems later on too.

Edit: Of course using this for my input here would require me to use it in other areas to, requiring a massive rewrite. I'm wondering if that update will be quicker and maybe even save me a lot more time in the long run, or if I can think of a nice solution for this using the method I was originally going with.
0

Share this post


Link to post
Share on other sites
[quote name='Gixugif' timestamp='1327126807' post='4904776']
I'm trying to implement many things myself that higher-level libraries[/quote]

Here is [url="http://unixpapa.com/js/key.html"]study of key event behavior[/url] in JavaScript. While JS adds another layer of abstraction, it's still closely tied to platform it's running on.

Just something to think about when trying to do something from scratch, let alone trying to do something portable. Each such issue can easily become a life-long full-time job.

The behavior is also not a bug as such, just different way of handling keyboard events.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0