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 02, 2007 11:47 PM by Jon Galloway
Filed under: , ,

Comments

# Silverlight Fan » Keyboard input tricks for Silverlight 1.1- Examples, Samples, Games, Tutorials and Videos about Silverlight

Pingback from  Silverlight Fan » Keyboard input tricks for Silverlight 1.1- Examples, Samples, Games, Tutorials and Videos about Silverlight

# 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 03, 2007 10:39 AM by Cam Soper

# Silverlight Cream for July 3, 2007

Silverlight Cream for July 3, 2007

Tuesday, July 03, 2007 12:11 PM by WynApse

# Jon Galloway's Keyboard Tricks in Silverlight

Nice post with code examples detailing how to capture keyboard events in Silverlight. He learned about

Tuesday, July 03, 2007 12:51 PM by Eric Griffin's Blog

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

The KeyDown events don't get fired for arrow keys under IE, but they do under Firefox. I'm thinking this will be fixed in the next release.

silverlight.net/.../2224.aspx

Tuesday, July 03, 2007 8:17 PM by Andy Beaulieu

# Silverlight Learning Links

Presentation Download will be available Soon... Getting Started Microsoft Silverlight 1.0 Beta Runtime

Thursday, July 05, 2007 5:18 AM by Technical Bits

# July 4th Links: ASP.NET, ASP.NET AJAX, Visual Studio, Silverlight and IIS7

I've fallen behind on my weekly link-listing series - apologies for the delay. ASP.NET ASP.NET RSSToolkit

Thursday, July 05, 2007 10:17 AM by Blogs

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

Hey Jon,

I've used the information on this blog post (and linked back here), Thanks.

blogs.microsoft.co.il/.../Silverlight-1.0-full-Javascript-Intellisense.aspx

Wednesday, August 01, 2007 8:40 AM by Justin-Josef Angel [MVP]

# Handling Keyboard Input in SilverLight « Online Advertising, Web Development & General Rantings

Pingback from  Handling Keyboard Input in SilverLight « Online Advertising, Web Development & General Rantings

# Silverlight 1.0 full Javascript Intellisense

Silverlight 1.0 full Javascript Intellisense

Thursday, August 02, 2007 5:29 PM by .NET On Rail

Leave a Comment

(required) 
(required) 
(optional)
(required)