The bulk of my work is performed via a Remote Desktop Connection. I will often listen to music on my local machine while working. I know this is a small thing but when I need to Pause my music I must first minimize my RDP session so that my keyboard macros will be applied to the local machine and not the remote machine.
I tried creating a simple c# application that will simulate the key presses for play/pause, next, previous but this doesn't solve my issue because it is still simulating a key press. My question is how can I control my music on my local machine without minimizing my RDP session?
Here is the code I am currently using, maybe I need a different approach?
{
[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;// code to jump to next track
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;// code to play or pause a song
public const int VK_MEDIA_PREV_TRACK = 0xB1;// code to jump to prev track
public const int VK_VOLUME_UP = 0xAF;
public const int VK_VOLUME_DOWN = 0xAE;
public const int VK_VOLUME_MUTE = 0xAD;
static void Main(string[] args)
{
var command = args[0];
switch (command) {
case "previous":
Console.WriteLine($"command = {command}");
// Jump to previous track
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
break;
case "play/pause":
Console.WriteLine($"command = {command}");
// Play or Pause music
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
break;
case "next":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
break;
case "up":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_VOLUME_UP, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
break;
case "down":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_VOLUME_DOWN, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
break;
case "mute":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
break;
default:
Console.WriteLine($"command = NOT RECOGNIZED");
break;
}
}
}```
I ended up getting the handle for my computers Desktop and passing that in the keydb_event.
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;// code to jump to next track
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;// code to play or pause a song
public const int VK_MEDIA_PREV_TRACK = 0xB1;// code to jump to prev track
public const int VK_VOLUME_UP = 0xAF;
public const int VK_VOLUME_DOWN = 0xAE;
public const int VK_VOLUME_MUTE = 0xAD;
static void Main(string[] args)
{
var command = args[0];
var handle = GetDesktopWindow();
switch (command) {
case "previous":
Console.WriteLine($"command = {command}");
// Jump to previous track
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, handle);
break;
case "play/pause":
Console.WriteLine($"command = {command}");
// Play or Pause music
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, handle);
break;
case "next":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, handle);
break;
case "up":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_VOLUME_UP, 0, KEYEVENTF_EXTENTEDKEY, handle);
break;
case "down":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_VOLUME_DOWN, 0, KEYEVENTF_EXTENTEDKEY, handle);
break;
case "mute":
Console.WriteLine($"command = {command}");
// Jump to next track
keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_EXTENTEDKEY, handle);
break;
default:
Console.WriteLine($"command = NOT RECOGNIZED");
break;
}