Windows – Generic way to remap every media button

autohotkeykeyboard shortcutswindows

Today I tried to remap some keyboard media keys to more useful functions and failed to do so.

enter image description here

The problem

  • The manufacturer doesn't provide a utility to bind and execute custom commands.
    My previous keyboard from Logitech had such options in their SetPoint software
  • The key codes (or scancode) were not recognized by most programs which I used to catch the underlying key codes.

    For example the otherwise excellent tool SharpKeys has a catch mode where it tells you what key you've just pressed. It took me some time to realize that it told me a wrong key code
    During my tests I pressed the "star button" in the top right and SharpKeys tells me that this is the key "D" with it's key code 00_20. But the correct key code would be 00_181

    Another try was this AutoHotKey script which also failed to recognize my unusual media keys

The task

  • Find the correct key codes even for unusual media buttons
  • Remap all media keys without any installation of an additional tool or program. A registry tweak, the use of an already installed driver or a small portable app is acceptable

Best Answer

This is a generic way to remap any keyboard buttons. Even weird ones


  1. Use KeyCodes (portable,415 KB) to catch the key code. Download, extract and start KeyCodes3.exe. Press your mysterious button and you'll see the key code as decimal number

    enter image description here

    An alternative way was this AHK script which uses a keyboard hook and "KeyHistory"

    #Persistent
    #InstallMouseHook
    #InstallKeybdHook
    while !(getKeyState("F1", "T"))
          KeyHistory
    return
    esc::exitapp
    

    enter image description here

  2. Convert the number from decimal to hexadecimal: 171 » AB

  3. Use the portable version of AutoHotKey to remap the button.
    Download and extract the .zip version of your choice somewhere. No installation needed.
    Create a new text file and paste

    #NoEnv, #Persistent, #SingleInstance
    vkB5::return                ;music node button  181 » B5   do nothing
    vkAC::return                ;home button        172 » AC   do nothing
    vkAA::return                ;search button      170 » AA   do nothing
    vkAB::Run, c:\myapp.exe     ;star button        171 » AB   execute tool
    vkB4::Send {Volume_Mute}    ;mail button        180 » B4   mute/unmute
    
    • vk stands for virtual key code followed by your hexadezimal key code you want to remap
    • :: is the remap operator. Left is the trigger, right of it is your new button or action
    • Send {<my_new_key>} could be any key from this list.
      Alternatively you could execute a custom path with the Run command.
      Or if you want to to execute a media button again, use {vkAB} as action
  4. Create a new shortcut in your autostart folder to start your AHK script on each boot. For example

    "D:\Tools\AutoHotKey\AutoHotkey.exe" "D:\Tools\AutoHotKey\RemapKeys.ahk"
    

After a reboot or manual AHK script start, all buttons are remapped

Related Question