13

I've got two keyboards attached to my computer. A normal full-size keyboard and a numeric keypad. I want to "remap" the keys on the numeric keypad, but leave the full keyboard alone. So, when the user presses "5" on the keypad it would get remapped to the "Media Play" key, but if the same "5" was pressed on the keypad of the full keyboard, I'd get a "5". In essence, I want to turn that seperate numeric keypad into a media control device.

Unfortunately I'm not sure how to make this work. There is a "Raw Input" feature of Windows which allows to distinguish between keyboards, but that only allows to read the keys - not remap them. There are keyboard hooks (low level and high level) which can remap the keystrokes, but they don't distinguish between keyboards.

Can I make a combination of the two methods work? For that I'd have to be sure that the raw input message is processed before the keyboard hook kicks in. Is that so?

Also, I've read that Windows for some reason does not allow to use Raw Input and Keyboard Hooks in the same process. I could work around that by making 2 different processes, but that becomes more fragile because of communication latency (keyboard hooks have very limited time in which they must complete their work).

Is this doable at all?

Vilx-
  • 104,512
  • 87
  • 279
  • 422

2 Answers2

7

In the end it turned out I needed a custom keyboard driver to accomplish this. Fortunately I found one ready for taking - Interception. The app is ready and works perfectly. :)

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • Is this app public? It sounds very useful. – Yary Apr 23 '15 at 02:53
  • @Yary - no, I haven't published it, but it needs a lot of polish to be useful to others. I've hardcoded both my keypad hardware ID, and the key mapping. In its current state, if you wanted to adapt it to your own needs, you might as well just write one from scratch. – Vilx- Apr 23 '15 at 07:12
  • 1
    @Yary - Still, if you want it, here you go: http://valts.21.lv/Mediaception.zip. Just remember that you need to install the driver too, which you can download from the Interception website above. – Vilx- Apr 23 '15 at 07:16
  • 2
    I found another project that uses the Interception driver, and discovers the info it needs to write an .ini-style configuration, it's very handy, I was up and running in about half an hour. [The interceptor multi-keyboard remapping binary and source code is on this _Orbiter_ forum post](http://orbiter-forum.com/showthread.php?t=30829), it requires registering with their forum to download. – Yary Apr 23 '15 at 13:05
  • I install the program, but I don't see where I can call it to remap my keyboard. – Andi Jay Sep 28 '16 at 22:36
  • @AndiJay - which program? My Mediaception, the Interception driver, or the thing on the Orbiter forum? – Vilx- Sep 29 '16 at 08:29
  • Running the `install-interception.exe` gives me `usage: C:\path\to\Interception\command line installer\install-interception.exe [/install | /uninstall]`. Any idea what those "instructions" mean ? – George Dimitriadis Jul 26 '17 at 08:32
  • 2
    Kids these days! You need to use the command line and run `install-interception.exe /install`. – Vilx- Jul 26 '17 at 09:19
  • I've spent days looking for a solution like that. Thank you so much. – Rodrigo Borba Jan 18 '20 at 19:03
  • @RodrigoBorba - Lately it's become unreliable on my Windows 10 machine. It often stops sending signals, and on occasion even locks down all keyboards entirely, so that not even a reboot helps. In that case you need to uninstall it (Windows on-screen keyboard still works), and reinstall. – Vilx- Jan 18 '20 at 21:26
  • Unfortunately I've found it out already. I kept looking for a better ready solution but i could't find one. I'm starting to developer a definitive solution. Maybe open source. I'll share the repository as soon as I get something ready. – Rodrigo Borba Jan 19 '20 at 16:10
  • @RodrigoBorba - The way Inception does it is via a special driver. And drivers are tricky. Writing them is one thing, but if you want to get it installed without much trouble, you'll need to get Microsoft's signature on it, and I don't know the process or expenses for that. – Vilx- Jan 19 '20 at 19:35
  • I have to study more about it, but I think it is possible to install a unsigned driver. As it would be a open source solution, it won't be very hard to check whether the code is malicious or not. – Rodrigo Borba Jan 19 '20 at 21:45
  • @RodrigoBorba - Sure, you can. After all - you need to be able to write them and test them, right? But there's hoops to jump through and it won't be end-user friendly. – Vilx- Jan 19 '20 at 22:33
  • That is right. At less we would have some sort of solution... – Rodrigo Borba Jan 19 '20 at 22:40
  • @RodrigoBorba - If you make it, let me know! Driver development is a dark mystery to me. :) – Vilx- Jan 19 '20 at 23:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206249/discussion-between-rodrigo-borba-and-vilx). – Rodrigo Borba Jan 20 '20 at 01:42
  • @RodrigoBorba is this chat still open? I'm looking for a solution to remap a single keyboard – AutoBaker May 24 '22 at 13:48
  • Is there any way to adapt the keyboard itself so that keys can be remapped on the keyboard side and completely skip the windows software? I guess this will vary from keyboard to keyboard but just to raise a thought process... – AutoBaker May 24 '22 at 13:51
  • I'm not trying to spam the comments, but just had a brainwave... if keyboard can't be remapped, maybe I can have an i/o device between keyboard and PC that simply changes the input to a different output... and it might be possible. I can set up a Pi as keyboard device, plug keyboard into pi, write code to turn one input into another. Got a Pi on order right now! https://randomnerdtutorials.com/raspberry-pi-zero-usb-keyboard-hid/#:~:text=After%20preparing%20your%20Raspberry%20Pi,keyboard%20to%20the%20connected%20computer. – AutoBaker May 24 '22 at 13:55
  • @5Diraptor - When you start playing with hardware, anything's possible. However I don't think that you will be able to simply take a consumer-grade keyboard and remap it. The chips in there aren't meant to be reprogrammed. But you could use parts of it... either replace the chip with a different one that you _can_ program, or keep the chip and make a custom keyboard. Both options however will be a lot of work. – Vilx- May 24 '22 at 19:22
  • @5Diraptor - The other option you mentioned is using some hardware in the middle. That is... umm... definitely possible, but I wouldn't know even where to start. For one thing you would need the PI (or whatever) to act both as a USB host and a USB slave. And you'd have to do some low level USB mungling... or find a software that lets PI emulate a keyboard - maybe that would be easier. Still plenty of work. – Vilx- May 24 '22 at 19:24
  • 1
    @5Diraptor - This is the reason why I wanted a software solution on the PC - because I judge that to be the easiest way. In principle there is already enough information coming in to do this - the question is just if the OS supports a way to do it. Maybe Linux has better facilities? I don't know. – Vilx- May 24 '22 at 19:43
  • 1
    @5Diraptor I actually dropped this approach, but I still think it is doable. I just do not have time to invest on it anymore. The idea behind is to have a drive that overrides the default browser for a specific UID of an USB device. Having a device in the middle is also a valid solution, but demands hardware – Rodrigo Borba May 25 '22 at 13:48
2

I wanted to expand on the answer by Vilx as I've revisited this post several times, and I've only just managed to remap my second keyboard so the keys act uniquely. This is done using Interception and Intercept together, and it's the only working solution that I've managed to get working so far.

I'm also looking into using a raspberry pi to remap a second keyboard. Although early days, I think it it's achievable: Plug keyboard into Pi, plug Pi into PC and write script to turn input keycodes into customized output commands / keycodes. Will update when I have tested.

My working Intercept solution is completely based on a Github tutorial by TaranVH which details the process the best so far. I've copied his solution below with some amends of my own.

DOWNLOADING INTERCEPTION:

  1. Navigate to this page: https://github.com/oblitum/Interception
  2. Click on "download the latest release."
  3. Click on Interception.zip to download it.
  4. Unzip the file to a folder on your computer - I recommend using a folder location that won't ever move. I put my Interception folder into C:\Program Files\ and I'll be using this location in my examples.

INSTALLING INTERCEPTION:

  1. Open a terminal as administrator and type cd "C:\Program Files\Interception\command line installer"
  2. Hit Enter
  3. Now, type in the following line of text exactly as shown: install-interception.exe /install
  4. Hit Enter

Once the program installs sucessfully, you still must restart your computer in order to complete the installation.

(If you ever want to UNinstall interception, watch this): https://www.youtube.com/watch?v=Hn18vv--sFY

INSTALLING INTERCEPT - (This is different from intercepTION!)

  1. Download Kamaz's intercept.exe zip from one of these locations:

    1. http://octopup.org/img/code/interception/intercept.zip
    2. https://github.com/TaranVH/2nd-keyboard/blob/master/Intercept/intercept.exe
  2. Unzip it if it's a .zip file and locate intercept.exe

  3. To keep these files all in one place, I moved intercept.exe into C:\Program Files\Interception\

  4. Reboot your system.

SETTING UP INTERCEPT

  1. Plug in your second keyboard and ensure that it is working normally.
  2. Go to where you saved intercept.exe and double click to open.
  3. Intercept will open as a terminal, and from there you should have basic instructions to start remapping your keyboard.

  1. Type a to add a key.

  2. On the SECONDARY keyboard, press the Q key once. (this is the key that you are remapping)

  3. You will be prompted with Enter combo for this trigger, end with ESC.

  4. Carefully execute the following keystrokes (it doesn't matter which keyboard): Win + E (That is: hold down the windows key while pressing the letter E. I've found you need to do this quickly and accurately, if you hold down too long, extra keypresses are added)

  5. When I did it, this was shown in the terminal: [Left Windows]↓ [E]↓ [E]↑ [Left Windows]↑

  6. Press Esc and you will be prompted to label the script

  7. I labelled mine Q->WinE so I knew what it was doing.

  8. Press Enter to accept the label

  9. Press S to save the filter, or C to cancel if you made a mistake.

  10. Add more filters or close the terminal.

USING INTERCEPT

Now you've set up a custom key command, you need to apply the filter to use it.

  1. Open up intercept.exe
  2. Press Y to apply the filters, you will get confirmation that the filters have been activated.
  3. Now, when you press Q on your second keyboard, Intercept will change the command to Win+E which will open File Explorer.

Caveat:

AutoBaker
  • 919
  • 2
  • 15
  • 31