How to simulate multimedia key press (in C)?

前端 未结 2 1402
夕颜
夕颜 2020-12-19 16:53

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 pr

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 17:14

    Even more easier than the accepted answer is keybd_event. No structs just a call with numeric parameters.

    // C# example:
    public const int KEYEVENTF_EXTENDEDKEY = 1;
    public const int KEYEVENTF_KEYUP = 2;
    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", SetLastError = true)]
    public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
    
    keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
    

提交回复
热议问题