Freestyle Coding

Programming in the Real World

  Home  |   Contact  |   Syndication    |   Login
  53 Posts | 0 Stories | 39 Comments | 0 Trackbacks

News

Tag Cloud


Archives

Post Categories

Charity

Conferences

Professional

Projects

Social Networks

NOTE: I created a better version of this control. There are some important facts in this post that you should read first. However, check out Improved Keyboard Buffer for Windows Store Apps for a much improved version.

As I mentioned in my previous post, I came across a situation where I needed to create a keyboard buffer for a Windows Store App. I will now explain why I needed to do this and how I did it.

Windows Store Apps do some strange things with the keyboard. There is a completely logical explanation for this. WinRT is designed to work on devices that do not have a permanently connected physical keyboard. Since the system has to pop up a virtual keyboard if one is not connected, it plays a little tight on keyboard controls. This can be a problem if you are creating a game that uses a physical keyboard as a valid input device.

The first thing I tried to do was overload the OnKeyUp and OnKeyDown event on my main window. However, this does not work. WinRT on first the keyboard events if the control with the active focus can use a keyboard, such as a TextBox. This is really a problem for a game; I don't really want a control of that type to be displayed on the screen.

A little digging led me to discover that the lowest level class that can use OnKeyUp and OnKeyDown was a Windows.UI.Xaml.Controls.Control. However, this guy does have to be on the screen. As such, I made him one pixel tall and one pixel wide. Then I blended it to the background color and hid it in the top left corner of the screen.

Next, I had to capture all the input. In my experience, it's best to tread lightly when stealing events from the OS. As such, I look for the keys the concern me, process them as necessary, and make them as handled. If I don't care about the key, I just pass him along to the OS. There was one exception to this. I actively look for an Alt-F4 key combination. This is because I wanted to actively call Application.Current.Exit(). If you let the OS shut the app down, it decides to take about 10 seconds for the application lifecycle to defer termination in case you want to save state. I don't want this, so I did it myself.

I also discovered that I actually need to keep track of the keys that were down. This problem received its own blog post. It should be the post right below this, entitled This is not the OnKeyDown you are looking for.

The code includes a reference to IControlState. This is an interface I created to allow all the different methods of controlling the game to have a common interface. You'll see him pop up in other posts as I add additional input methods. I am omitting the code for him, but it's the same interface in my XNA talks and samples.

public class KeyboardBuffer : Control, IControlState {
private bool AltPressed = false;
private List<VirtualKey> PressedKeys;
 
public KeyboardBuffer() {
this.Width = 1;
this.Height = 1;
Movement = new Point( 0, 0 );
PressedKeys = new List<VirtualKey>();
}
 
protected override void OnKeyDown( Windows.UI.Xaml.Input.KeyRoutedEventArgs e ) {
if( !PressedKeys.Contains( e.Key ) ) {
PressedKeys.Add( e.Key );
switch( e.Key ) {
case VirtualKey.Up:
Movement = new Point( Movement.X, Movement.Y - 5 );
e.Handled = true;
break;
 
case VirtualKey.Down:
Movement = new Point( Movement.X, Movement.Y + 5 );
e.Handled = true;
break;
 
case VirtualKey.Left:
Movement = new Point( Movement.X - 5, Movement.Y );
e.Handled = true;
break;
 
case VirtualKey.Right:
Movement = new Point( Movement.X + 5, Movement.Y );
e.Handled = true;
break;
 
case VirtualKey.Space:
m_Fire = true;
e.Handled = true;
break;
 
case VirtualKey.F12:
case VirtualKey.Pause:
Pause = !Pause;
e.Handled = true;
break;
 
case VirtualKey.Escape:
Exit = true;
e.Handled = true;
break;
 
case VirtualKey.Menu:
AltPressed = true;
break;
case VirtualKey.F4:
if( AltPressed )
Exit = true;
 
e.Handled = true;
break;
}
}
 
base.OnKeyDown( e );
}
 
protected override void OnKeyUp( Windows.UI.Xaml.Input.KeyRoutedEventArgs e ) {
while( PressedKeys.Remove( e.Key ) );
 
switch( e.Key ) {
case VirtualKey.Up:
Movement = new Point( Movement.X, Movement.Y + 5 );
e.Handled = true;
break;
 
case VirtualKey.Down:
Movement = new Point( Movement.X, Movement.Y - 5 );
e.Handled = true;
break;
 
case VirtualKey.Left:
Movement = new Point( Movement.X + 5, Movement.Y );
e.Handled = true;
break;
 
case VirtualKey.Right:
Movement = new Point( Movement.X - 5, Movement.Y );
e.Handled = true;
break;
 
case VirtualKey.Menu:
AltPressed = false;
break;
}
 
base.OnKeyUp( e );
}
 
#region IControlState
public Point Movement {
get;
private set;
}
 
private bool m_Fire = false;
public bool Fire {
get {
bool _ReturnValue = m_Fire;
m_Fire = false;
return _ReturnValue;
}
}
 
public bool Pause {
get;
private set;
}
 
public bool Exit {
get;
private set;
}
#endregion
}

Soon, I'll show you how I'm using this guy to move my sprite. However, this post is long enough, and the next part will involve lots of threading. Let's leaving things well enough for now.

As always, if you have nay questions about the code up there, leave a comment. I'm sure there are a few strange things in there that I'm just not remembering at the moment.

posted on Saturday, January 26, 2013 12:06 PM

Feedback

# re: Creating a Keyboard Buffer in Windows Store Apps 1/31/2013 8:21 AM Kamrul Islam
It has become a crucial article for me as I was looking for it. Thanks a lot for sharing.

# re: Creating a Keyboard Buffer in Windows Store Apps 1/31/2013 11:40 AM writing services
Thanks for the bunch of good resourceful site.I really appreciate your blog,you have done the great job.hey your blog design is very nice, clean and fresh and with updated content, make people feel peace and I always like browsing your site...

# re: Creating a Keyboard Buffer in Windows Store Apps 1/31/2013 11:44 AM dissertation
I really enjoyed reading it and I think need to read more on this topic.Thanks a lot for sharing. Keep blogging

Post A Comment
Title:
Name:
Email:
Comment:
Verification: