Some keyboard input tricks for Silverlight 1.1 (Alpha) - Jon Galloway

Some keyboard input tricks for Silverlight 1.1 (Alpha)

Here are a few tricks I learned while doing my "hello world" maze game in Silverlight 1.1.

Silverlight doesn't fire the KeyDown event for cursor (arrow) keys

However, just about all keys will fire a KeyUp event. In my case, I was able to just handle the KeyUp event, but remember that holding a key down will trigger multiple KeyDown events and only one KeyUp event.

Notice how a lot of the games you're seeing for Silverlight use W/A/S/D instead of arrow keys? I suspect that's the reason.

The difference between Key and PlatformKey

The KeyDown and KeyUp event parameters include KeyboardEventArgs, which has properties for both PlatformKeyCode and Key. PlatformKeyCode is a platform specific key code, so by definition it can (and will) change between Windows and Mac (and potentially between versions of these operating systems). It's preferable to use Key unless you need to be you need to detect keys which aren't available on all platforms, such as the Windows start key.

Key is a platform agnostic key code and is better to use when possible. Of course, there isn't a Key value for the Windows key, so if you're using keys which will only be defined on a specific operating system you're stuck with the PlatformKeyCode value. If you do need access to PlatformKey values, see Dave Relyea's comment on this thread for info on how he determined platform key values.

The missing Key Enumeration

KeyboardEventArgs.Key value is supposed to be an Enum per the MSDN documentation, but at least in this release it's just an int. I grabbed the keys and values from the documentation and created a simple enum which should hopefully be forward compatible when that enum is (hopefully) added to agclr.dll.

enum Keys { KEYNONE = 0, BACKSPACE = 1, TAB = 2, ENTER = 3, SHIFT = 4, CTRL = 5, ALT = 6, CAPSLOCK = 7, ESCAPE = 8, SPACE = 9, PAGEUP = 10, PAGEDOWN = 11, END = 12, HOME = 13, LEFT = 14, UP = 15, RIGHT = 16, DOWN = 17, INSERT = 18, DELETE = 19, DIGIT0 = 20, DIGIT1 = 21, DIGIT2 = 22, DIGIT3 = 23, DIGIT4 = 24, DIGIT5 = 25, DIGIT6 = 26, DIGIT7 = 27, DIGIT8 = 28, DIGIT9 = 29, A = 30, B = 31, C = 32, D = 33, E = 34, F = 35, G = 36, H = 37, I = 38, J = 39, K = 40, L = 41, M = 42, N = 43, O = 44, P = 45, Q = 46, R = 47, S = 48, T = 49, U = 50, V = 51, W = 52, X = 53, Y = 54, Z = 55, F1 = 56, F2 = 57, F3 = 58, F4 = 59, F5 = 60, F6 = 61, F7 = 62, F8 = 63, F9 = 64, F10 = 65, F11 = 66, F12 = 67, NUMPAD0 = 68, NUMPAD1 = 69, NUMPAD2 = 70, NUMPAD3 = 71, NUMPAD4 = 72, NUMPAD5 = 73, NUMPAD6 = 74, NUMPAD7 = 75, NUMPAD8 = 76, NUMPAD9 = 77, MULTIPLY = 78, ADD = 79, SUBTRACT = 80, DECIMAL = 81, DIVIDE = 82, KEYUNKNOWN = 255 }

The result: a generic Keyboard Handler Event

First, I add the event handler in the Page_Load event:

public void Page_Loaded(object o, EventArgs e) { InitializeComponent(); this.KeyUp += new System.Windows.Input.KeyboardEventHandler(keyHandler); //Other stuff... }

Next, my keyHandler method has a simple switch block which allows for W/A/S/D, numeric keypad, or cursor keys. I also handle the F key, which toggles to full screen mode:

protected void keyHandler(object sender, KeyboardEventArgs args) { Keys key = (Keys)args.Key; switch (key) { case Keys.A: case Keys.NUMPAD4: case Keys.LEFT: handleLeft(); break; case Keys.W: case Keys.NUMPAD8: case Keys.UP: handleUp(); break; case Keys.D: case Keys.NUMPAD6: case Keys.RIGHT: handleRight(); break; case Keys.S: case Keys.NUMPAD2: case Keys.DOWN: handleDown(); break; case Keys.F: BrowserHost.IsFullScreen = !BrowserHost.IsFullScreen; break; default: return; } }
Published Monday, July 2, 2007 11:47 PM by Jon Galloway
Filed under: , ,

Comments

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

The arrow keys don't fire KeyDown events?  That seems odd.  Do you suppose that's by design, or an oversight?

Tuesday, July 3, 2007 10:39 AM by Cam Soper

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

103 web high home release indicate service

Saturday, May 30, 2009 3:51 PM by stabilized conclude fuels

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

deep responsible forcings google running basis case trading

Friday, June 5, 2009 2:46 AM by geological lapse era

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

Hi, interest post. I'll write you later about few questions!

Friday, June 12, 2009 4:56 PM by Kelly Brown

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

videos observed temperatures intergovernmental read sunlight external

Sunday, June 21, 2009 2:03 PM by business warmer late study

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

Very informative article

Wednesday, June 24, 2009 4:54 PM by redlelidito

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

without approximately impact developed geological

Thursday, July 2, 2009 3:14 PM by country rise panel page ces

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

It's a masterpiece. I have never thought people can have such ideas and thoughts. You are great.

Monday, July 6, 2009 3:05 PM by CrisBetewsky

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

tks for the effort you put in here I appreciate it!

Monday, July 20, 2009 11:44 AM by MichaellaS

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

So where it to find?,

Wednesday, July 29, 2009 4:11 PM by name

# re: Some keyboard input tricks for Silverlight 1.1 (Alpha)

hi guys,new UK bodybuilder here, looks like a decent forum with lots of good info - hopefully i can contribute & learn.

Tuesday, September 15, 2009 6:34 AM by brucejohnson