// releases a key private void KeyUp(ushort scanCode) { INPUT[] inputs = new INPUT[1]; inputs[0].type = WMP.INPUT_KEYBOARD; inputs[0].ki.wScan = scanCode; inputs[0].ki.dwFlags = WMP.KEYEVENTF_KEYUP | WMP.KEYEVENTF_SCANCODE; uint intReturn = WMP.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])); if (intReturn != 1) { throw new Exception("Could not send key: " + scanCode); } }
// press or release a key ch based on the boolean press private void PressKey(char ch, bool press) { byte vk = WMP.VkKeyScan(ch); ushort scanCode = (ushort)WMP.MapVirtualKey(vk, 0); if (press) { KeyDown(scanCode); } else { KeyUp(scanCode); } }
private bool focusApp(string app) { if (!checkBuffer()) { stopWatch.Start(); return(false); } Process[] processes = Process.GetProcessesByName(app); if (processes.Length == 0) { stopWatch.Start(); return(false); } IntPtr WindowHandle = processes[0].MainWindowHandle; WMP.SwitchWindow(WindowHandle); return(true); }