2

Is there a way for my program to be able to "fire" the special keys on my keyboard, such as the media keys (play, pause, next etc..)?

I'm trying to create an app that performs those functions and an easy way would be to simulate those keys being pressed.

This is a personal project so I don't mind if the solution isn't generic and/or won't work for all keyboards.

I've looked around and I think SendKey might be one way to go, but I don't know what the scan codes are for these special keys.

DarkTrick
  • 2,447
  • 1
  • 21
  • 39
Skoder
  • 3,983
  • 11
  • 46
  • 73

1 Answers1

9

The following link provided the great idea to using the multimedia keyboard keys in your application but this article is for c++, you have to find out by yourself to how should to use those functions.

EDIT : Based on these articles, I implemented some of media function key which lets you to control volume from your application.

    private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
    private const int APPCOMMAND_VOLUME_UP = 0xA0000;
    private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
    private const int WM_APPCOMMAND = 0x319;
    private const int APPCOMMAND_MEDIA_PLAY_PAUSE = 0xE0000;


    [DllImport("user32.dll")]
    public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
        IntPtr wParam, IntPtr lParam);

    public Form1()
    {
        InitializeComponent();
    }

    private void btnPlayPause_Click(object sender, EventArgs e)
    {
        SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
            (IntPtr)APPCOMMAND_MEDIA_PLAY_PAUSE);
    }

    private void btnMute_Click(object sender, EventArgs e)
    {
        SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
            (IntPtr)APPCOMMAND_VOLUME_MUTE);
    }

    private void btnDecVol_Click(object sender, EventArgs e)
    {
        SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
            (IntPtr)APPCOMMAND_VOLUME_DOWN);
    }

    private void btnIncVol_Click(object sender, EventArgs e)
    {
        SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
            (IntPtr)APPCOMMAND_VOLUME_UP);
    }

Just copy and use wherever you want.

Low-Level Keyboard Hook in C#

Using Multimedia Keyboard Keys in Your Own Program

Saber Amani
  • 6,409
  • 12
  • 53
  • 88
  • +1 Thanks for the links. I have no experience with C++ so not sure how to include that. I got the C# code working, but how would I 'send' that key to the system to make it seem as if the user pressed it? – Skoder Aug 24 '11 at 20:59
  • 1
    can you share the other fonctionalite buttons like play next, play previous,stop...... thank you – Youness Feb 11 '15 at 14:10