Come per simulare multimediale premere il tasto (in C)?
Domanda
tastiere moderni hanno tasti multimediali speciali, per esempio 'Pause / Play' o 'Open Web Browser'. E 'possibile scrivere un programma che "presse" queste chiavi?
io preferirei soluzione in C, ma vorrei accettare una soluzione agnostica lingua, anche.
Soluzione
Utilizzare l'API SendInput Windows, se si parla di programmazione in Win32.
È necessario costruire strutture INPUT, impostando il membro del tipo a INPUT_KEYBOARD. Nella membro ki (tipo KEYBDINPUT), è possibile impostare VK (tasto virtuale) al codice VK desiderata (ad esempio, VK_MEDIA_NEXT_TRACK, VK_MEDIA_STOP).
codici chiave virtuali: http: // msdn .microsoft.com / en-us / library / dd375731 (v = VS.85) aspx
FunzioneSendInput: http: // MSDN. microsoft.com/en-us/library/ms646310(v=VS.85).aspx
Non ho verificato le seguenti, ma dovrebbe essere simile a questo:
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));
Altri suggerimenti
Ancora più facile che la risposta accettata è keybd_event . Non solo le strutture di una chiamata con parametri numerici.
// 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);