Matthew Schlachter

Mapping F12 to Play/Pause on a Lenovo Laptop using Lenovo Vantage on Windows 10

Lenovo laptops have many functions mapped to the function keys on the top row, but (at least on my laptop) media controls are not among them.

A lenovo keyboard, centered on the function keys row
Media controls (play/pause and next/previous) are notably absent from my Lenovo laptop keyboard

Lenovo laptops have a “User-defined Key” on F12 which can be mapped to a limited number of functions through the Lenovo Vantage application. Although media controls aren't among the selectable functions, there is one option that we can use to our advantage: Open applications or files.

Screen capture of the User-defined Key controls in the Lenovo Vantage app, showing the Open applications or files option selected
The “Open applications or files” option for the User-defined key allows us to perform custom actions

The “Open applications or files” option for the User-defined key allows us to perform custom actions

We’re going to create a simple C# application that triggers a virtual keyboard event for the media play/pause button present on some keyboards when the application is open and then configure Lenovo Vantage to open it when the F12 key is pressed. This allows us to have a play/pause button on our keyboard.

In your plain text editor of choice (I used Notepad++) create a file called f12-play-pause.cs; this will be the source code for our application. In it, add the following text:

using System;
using System.Runtime.InteropServices;

public class Program
{
    public const int KEYEVENTF_EXTENDEDKEY = 1;
    public const int KEYEVENTF_KEYUP = 0;
    public const int VK_MEDIA_PLAY_PAUSE = 0xB3;

    [DllImport("user32.dll")]
    public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);

    public static void Main()
    {
        keybd_event(VK_MEDIA_PLAY_PAUSE, KEYEVENTF_KEYUP, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
    }
}

When this program is run, it’ll execute the Main() method, which has one command: trigger a keyboard event for the MEDIA_PLAY_PAUSE key. Once this single command is run, the application is closed and you’re returned to what you were doing before. In practice, this happens so quickly that there’s virtually no interruption and the only indication that an application is being opened for this is a slight flicker in the Taskbar.

Now that we have the source code for our application, we need to compile it into an exe file. If you don’t already have a C# development set up, the easiest way I’ve found to compile this application is to use CS-Script.

In order to compile the script using CS-Script you first need to install it; this can be easily done by downloading the latest release from Github, extracting the folder (you might also need to install 7-Zip to open the archive), and double-clicking install.cmd. Once we’re done it can be easily uninstalled by double-clicking uninstall.cmd if you won't be needing it again.

Now that we have CS-Script installed, we can open a cmd window and navigate to the folder where we saved f12-play-pause.cs. First test that your script is working correctly by running:

cscs f12-play-pause.cs

Once you're sure that the script runs correctly using this command, run the following command to compile it to a file (-e stands for export, in that it's exporting the compiled application to a file instead of just running it):

cscs -e f12-play-pause.cs

This will generate a new file called f12-play-pause.exe, which should simulate a play/pause button press when opened. Make sure this application is working properly and move it to somewhere you’re okay with keeping it permanently.

Finally, it’s time to connect our new application to the F12/User-defined key. Open Lenovo Vantage and select “Input & accessories” from the Device dropdown.

The dropdown where the User-defined Key settings are found in Lenovo Vantage
The dropdown where the User-defined Key settings are found in Lenovo Vantage

Scroll down and select “Open applications or files” option in the Action User Defined Key dropdown. You’ll now be presented with two options: Applications to open and Files to open. Interestingly, our new application isn’t available through the Applications option, so we need to instead click on the Add button in the “Files to open” section and select our f12-play-pause.exe application.

Our application won't display in the list unless you restart Lenovo Vantage (after which it'll appear as a nameless entry in the Applications list), but now when you click F12 on your keyboard your music will toggle between play and pause.

I am, of course, always looking for ways to improve this setup; feel free to add any comments, questions, or suggestions you might have in the comments section below.

Comments