private List <KeyInfo> GetKeyboardKeys(KeyStroke keyStroke) { var output = new List <KeyInfo>(); var corrected = KeysHelper.GetCorrectedKey(keyStroke.Code, keyStroke.State); bool isDown = keyStroke.State == KeyState.Down || keyStroke.State == (KeyState.Down | KeyState.E0); output.Add(new KeyInfo(corrected, isDown)); return(output); }
public Interception(KeyboardFilterMode keyboardFilter, MouseFilterMode mouseFilter) { KeysHelper.CheckInterceptionKeysForDuplicates(); this.KeyboardFilterMode = keyboardFilter; this.MouseFilterMode = mouseFilter; if (MouseFilterMode == Enums.MouseFilterMode.None) { Interception.DisableMouseEvents = true; } this.hardwareId = new StringBuilder(Interception.HardwareIdSize); this.mouseWheelAutoOffDelay = TimeSpan.FromMilliseconds(50); this.mouseMoveAutoOffDelay = TimeSpan.FromMilliseconds(50); this.KeyPressDelay = 1; this.ClickDelay = 1; this.ScrollDelay = 15; Interception.MouseMoveDeadZone = 1; // Initializing keyStates (false for every key on every device) this.keyStates = new Dictionary <string, Dictionary <InterceptionKey, bool> >(); for (int i = 1; i <= Interception.MaxDeviceCount; i++) { string deviceStrongName; if (NativeMethods.IsKeyboard(i) > 0) { deviceStrongName = new InterceptionKeyboard(Convert.ToUInt16(i), "dump").StrongName; } else { deviceStrongName = new InterceptionMouse(Convert.ToUInt16(i), "dump").StrongName; } var states = new Dictionary <InterceptionKey, bool>(); foreach (InterceptionKey key in Enum.GetValues(typeof(InterceptionKey))) { states.Add(key, false); } this.keyStates.Add(deviceStrongName, states); } this.devices = new Dictionary <int, InterceptionDevice>(); }
private void DriverCallback() { NativeMethods.SetFilter(this.context, NativeMethods.IsKeyboard, (int)this.KeyboardFilterMode); NativeMethods.SetFilter(this.context, NativeMethods.IsMouse, (int)MouseFilterMode); Stroke stroke = new Stroke(); while (NativeMethods.Receive(this.context, this.deviceId = NativeMethods.Wait(this.context), ref stroke, 1) > 0) { bool isKeyboard = NativeMethods.IsKeyboard(this.deviceId) > 0; if (!isKeyboard) { if (Interception.DisableMouseEvents) { if (!Interception.SwallowMouse) { this.Send(this.context, this.deviceId, ref stroke, 1); } continue; } } // Checking if the input device is already in the device list. If not - adding it. if (this.devices.ContainsKey(this.deviceId)) { this.currentDevice = this.devices[this.deviceId]; } else { continue; } // Getting the keys if (isKeyboard) { this.currentKeys = this.GetKeyboardKeys(stroke.Key); } else { this.currentKeys = this.GetMouseKeys(stroke.Mouse); } // Saving the key states foreach (var keyInfo in this.currentKeys) { this.keyStates[this.currentDevice.StrongName][keyInfo.Key] = keyInfo.IsDown; } if (!isKeyboard && stroke.Mouse.Rolling != 0) { foreach (var keyInfo in this.currentKeys) { if (KeysHelper.IsMouseWheelKey(keyInfo.Key)) { this.ReleaseKeyWithDelay(this.currentDevice, keyInfo.Key, this.mouseWheelAutoOffDelay); } } } // Raising the event, which determines whether the input will be blocked or not. if (this.InputActivity != null) { var args = new InterceptionEventArgs(this.currentDevice, this.currentKeys); this.InputActivity(this, args); if (args.Handled) { continue; } } this.Send(this.context, this.deviceId, ref stroke, 1); } this.Unload(); throw new Exception( "Interception.Receive() failed for an unknown reason. The driver has been unloaded."); }