5

Is there some way that I can send multimedia control commands like next song, pause, play, vol up, etc. to the operating system? Commands that are sent when pressing Fn + some mapped ..key. I am making a remote control for PC and sending those commands is essential.

3
  • 1
    The class SendKeys will send commands to an existing program. Commented Feb 21, 2013 at 22:40
  • 1
    See stackoverflow.com/questions/8986417/… Commented Feb 21, 2013 at 22:41
  • Yea but that doesn't quite work. When i use SendKeys.Send("{MediaNextTrack}"); i get an error "Keyword "MediaNextTrack" is not valid." is using {} brackets right way? Commented Feb 21, 2013 at 22:59

4 Answers 4

4

You can use keybd_event to simulate keys presses, you have to simulate key down and then key up in order to recognize correctly

 [DllImport("user32.dll", SetLastError = true)] public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); public const int VK_MEDIA_NEXT_TRACK = 0xB0; public const int VK_MEDIA_PLAY_PAUSE = 0xB3; public const int VK_MEDIA_PREV_TRACK = 0xB1; public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag private void ButtonClick(object sender, EventArgs e) keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_KEYUP, IntPtr.Zero); }` 
Sign up to request clarification or add additional context in comments.

1 Comment

this example, sending both keydown and keyup events gives me erratic behavior. I posted an answer with my implementation, which gives me consistent functionality.
3

Actually, the answer of dxramax gives me erratic behavior. I'm posting this answer that gives me consistent behavior, and also has some more details.

To send multimedia keys, including Play/Pause, NextTrack, PrevTrack, etc, you can use keybd_event:

public class Program { public const int KEYEVENTF_EXTENTEDKEY = 1; public const int KEYEVENTF_KEYUP = 0; public const int VK_MEDIA_NEXT_TRACK = 0xB0; public const int VK_MEDIA_PLAY_PAUSE = 0xB3; public const int VK_MEDIA_PREV_TRACK = 0xB1; [DllImport("user32.dll")] public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); public static void Main(string[] args) { keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // Play/Pause //keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // PrevTrack //keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // NextTrack } 

Here is a list to the supported key codes that this windows api can handle:
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

The SendKeys class is very nice, but it's also limited. The approach above sends the key command directly to Windows OS.

1 Comment

According Microsoft, to achieve this you should instead use SendInput function. But it seem to work anyway, and at least the code to implement it is very short.
1

If anyone wonders on this page like me. All posts above do not work, you also need bScan which is second parameter, you can get it with MapVirutalKey(VKCode,0).

Ex:

 int keyValue = VK_MEDIA_NEXT_TRACK; keybd_event(keyValue, MapVirtualKey(keyValue, 0), KEYEVENTF_KEYUP, 0); keybd_event(keyValue, MapVirtualKey(keyValue, 0), KEYEVENTF_KEYUP, 0); 

Comments

0

Unfortunately, in most cases, keys Fn can't be sent using Windows API and as a result - using .NET classes. It depends on how the manufacturer has done this functionality. Probably it is supported by additional driver or even go over operation system.

You can check if it's possible to send Fn commands from the code by trying to hook them using Windows API code or some application like AutoHotKey. For instance, on my laptop, I can't hook multimedia commands.

Otherwise, if you are lucky, use SendKeys as mentioned in the comments.

2 Comments

Yea thats just my problem, i can't send combination of Fn + something, i guess that Fn key is not hooked up directly to OS, but its more of a hardware key something that is hooked to a keyboard controller that changes key code that will be sent to OS. Anyway my primary idea is not to send a keyboard combination but that event (or whatever it is) that causes system to volume up or down.
@Milan, such events are very dependable on manufacturer of device. You need to reuse drivers somehow. I don't believe that there is any documentation for that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.