Remapping Surface Pen Button Presses

Posted 2016/10/08. Last updated 2016/10/08.

Introduction

This past summer I was lucky enough to intern at Microsoft Research. As an added bonus, the Intern Signature Event was held at the Space Needle, with performances by Duke Dumont and Ellie Goulding, and a Surface Book as the surprise gift! Who could ask for more?

As I have been using my Surface Book more and more, I realized that the Surface Pen it comes with would be perfect as a presentation clicker/remote. The Surface Pen has a button with three distinct type of key presses (single, double, and long), but by default they can only be remapped in a limited way, mostly to open certain apps. However, I wanted to be able to use two of the presses as Page Up/Page Down to move through presentation slides or through a page while browsing, which is not possible with the current software version. People have already figured out how to achieve more complex remappings using AutoHotkey scripts, but I wanted a way to do it without third-party software. In this post, I explain how I managed to do so, by using a Registry hack.

Background

As this post on Reddit explains, presses of the Surface Pen correspond to Function Key presses. Moreover, each key press from a keyboard corresponds to a scancode, which can be changed in software to remap its functionality. For Windows, this is done by changing the registry. Utilities such as SharpKeys make this process transparent to the user, but I wanted to learn how it works under the hood, so I found an article explaining the registry hack in detail.

As it turns out, the Scancode Map value (of type REG_BINARY) under the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout is responsible for this remapping. It consists of 2 null DWORDs (4 bytes each) at the beginning, a null DWORD at the end, and then the number of changes and the changes in between, in little endian format. For example, the value

00000000 00000000 02000000 YYYYXXXX 00000000

maps the scancode XXXX into YYYY. Note that the third DWORD is actually one more than the number of keys we remap, so it is 2 in this case.

The Hack

Armed with the above, we just need to figure out the scancodes that correspond to the Surface Pen key presses, and the scancodes we want to map them to. Finding the scancode for Page Up/Down was not hard, but function keys above F12 are a bit trickier, and all values I found in my initial searches did not work. By chance, I came across this file, which actually contained the right values, which are summarized in the table below:

Pen Press Function Key Scancode Remapped Key Scancode
Single F20 6B Page Down 51
Double F19 6A Page Up 49
Long F18 69 - -

As a result, to remap a single press to Page Down and a double press to Page Up, we simply need to set Scancode Map to

00000000 00000000 03000000 51006B00 49006A00 00000000

where to use 2 bytes and respect endianness, a scancode XX is represented as XX00. Log out (or better yet, reboot), and the pen should now have the desired functionality!

Conclusion

Though there are more powerful and easier alternatives for re-purposing Surface Pen button presses, doing so without third-party software by relying on the internals of the Windows Registry gives you a sense of accomplishment. Though I used this technique for presentations, I hope that this post was sufficiently clear to allow you to modify it for your own purposes!