private void ProcessingMethod() { Microsoft.DirectX.DirectInput.Device keyboard = null; try { // utworzenie obiektu do odczytywania stanu klawiatury keyboard = new Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Keyboard); keyboard.SetDataFormat(Microsoft.DirectX.DirectInput.DeviceDataFormat.Keyboard); keyboard.SetEventNotification(_event); keyboard.SetCooperativeLevel(IntPtr.Zero, Microsoft.DirectX.DirectInput.CooperativeLevelFlags.Background | Microsoft.DirectX.DirectInput.CooperativeLevelFlags.NonExclusive); keyboard.Acquire(); while (_working) { _event.WaitOne(); if (!_working) { break; } Microsoft.DirectX.DirectInput.KeyboardState state = keyboard.GetCurrentKeyboardState(); for (int i = 0; i < _spiesVariables.Length; i++) { _spiesVariables[i].CheckState(state); } _event.Reset(); if (!_working) { break; } } } catch (ThreadAbortException) { } catch (Exception ex) { _log.Log(this, ex.ToString()); } finally { if (keyboard != null) { try { keyboard.Unacquire(); } catch { } try { keyboard.Dispose(); } catch { } keyboard = null; } } }
private void timer1_Tick(object sender, EventArgs e) { //if (!_listening) //{ // return; //} try { Microsoft.DirectX.DirectInput.KeyboardState state = _keyboard.GetCurrentKeyboardState(); for (int i = 0; i < checkedListBox1.Items.Count; i++) { KV kv = (KV)checkedListBox1.Items[i]; if (state[kv.Key] && !checkedListBox1.CheckedItems.Contains(kv)) { checkedListBox1.SetItemChecked(i, true); } } } catch { } }
//private bool _lastState = false; public void CheckState(Microsoft.DirectX.DirectInput.KeyboardState state) { bool lastState = _downs == Keys.Length; bool s = false; for (int i = 0; i < Keys.Length; i++) { s = state[Keys[i]]; if (_state[i] != s) { _state[i] = s; if (s) { _downs++; } else { _downs--; } } } bool currentState = _downs == Keys.Length; if (lastState != currentState) { if (Repeat) { if (currentState) { if (!_repeating) { _repeatingState = true; OnChangeValue(true); _timer.Change(RepeatAfter, System.Threading.Timeout.Infinite); _repeating = true; } } else { if (_repeating || lastState) { _repeating = false; _timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); OnChangeValue(false); } } } else { if (currentState) { // naciśnięto klawisze OnChangeValue(true); } else { // zwolniono klawisze OnChangeValue(false); } } } }