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; } }

11 Comments

Comments have been disabled for this content.