iliak's picture

Detect new key press on OpenTK.Input.KeyboardState

Project:The Open Toolkit library
Version:all versions
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:open
Description

Adds methods to detect if a new key press occured between the last call. You can have a look at my framework for an example (http://code.google.com/p/arcadeengine/source/browse/trunk/Engine/Input/K...).

Here's some examples :

public static bool IsKeyPress(Keys key)
{
	return CurrentState[(int)key];
}
 
public static bool IsNewKeyPress(Keys key)
{
	return CurrentState[(int)key] && !PreviousState[(int)key];
}
 
public static bool IsNewKeyUp(Keys key)
{
	return !CurrentState[(int)key] && PreviousState[(int)key];
}

Look at the Update() method to understand the story under the hood


Comments

iliak's picture

#1

Could be the same for Mouse too...

the Fiddler's picture

#2

Thanks for the code, this looks pretty useful.

Actually, I don't think this belongs to KeyboardState but rather to a 'helper' class that can compare two KeyboardStates and give you the difference. The KeyboardDevice class would be a good fit. The issue is that adding a PreviousState field to KeyboardState will open the door to all kinds of race conditions.

In any case, this is blocked by issue #969: [Input] Add static classes for Mouse, Keyboard and GamePad input, so it will take some time to implement.

iliak's picture

#3

Don't have time to look at the issue 969, why would it add a race condition ? Maybe you could add this feature to the Game class and update Keyboard / Mouse / GamePad states in in the Update method of this class...

the Fiddler's picture

#4

Imagine you have two threads calling Keyboard.GetState(). Each thread will thrash the IsNewKeyPress data of the other thread.

iliak's picture

#5

So it would be a nice feature in the Game class... If I have time (not sure at all) i'll post a pach for the svn.

the Fiddler's picture

#6

I'd wait until the new input API is implemented first (issue #969: [Input] Add static classes for Mouse, Keyboard and GamePad input).