Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate multimedia key press (in C)?

Modern keyboards have special multimedia keys, e.g. 'Pause/Play' or 'Open Web Browser'. Is it possible to write a program that "presses" these keys?

I would prefer solution in C, but I would accept a language agnostic solution, too.

like image 936
Tom Pažourek Avatar asked Jun 03 '10 19:06

Tom Pažourek


People also ask

What are multimedia hotkeys?

A special key or media key, or multimedia key is a keyboard key that performs a special function not included with the traditional 104-key keyboard. For example, the picture shows a Logitech keyboard. You can see that the first four buttons shown control the volume of the speakers and the computer's brightness.

How do I use media key?

How to Use Your Media Keys on Websites. Using your media keys should be simple: Just press them. For example, if you're playing a YouTube video and it's hidden in a background tab somewhere, you can press the Play/Pause key on your keyboard to pause it and press the key again to resume it. It's that simple.


1 Answers

Use the SendInput Windows API, if you are talking about programming under Win32.

You need to build INPUT structures, setting the type member to INPUT_KEYBOARD. In the ki member (KEYBDINPUT type), you can set vk (virtual key) to your desired VK code (for example, VK_MEDIA_NEXT_TRACK, VK_MEDIA_STOP).

Virtual key codes: http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx

SendInput Function: http://msdn.microsoft.com/en-us/library/ms646310(v=VS.85).aspx

I've not tested the following, but should be like this:

KEYBDINPUT kbi;
kbi.wVk = VK_MEDIA_STOP; // Provide your own
kbi.wScan = 0;
kbi.dwFlags = 0;  // See docs for flags (mm keys may need Extended key flag)
kbi.time = 0;
kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

INPUT input;
input.type = INPUT_KEYBOARD;
input.ki   = kbi;

SendInput(1, &input, sizeof(INPUT));
like image 166
Hernán Avatar answered Sep 24 '22 15:09

Hernán