Beispiel #1
0
        /// <summary>
        ///     Terminates this instance.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        ///     The " + nameof(InputManager) + " needs to be initialized before it can be
        ///     terminated.
        /// </exception>
        public void Terminate()
        {
            lock (_lockObject)
            {
                if (!IsInitialized)
                {
                    throw new InvalidOperationException("The " + nameof(InputManager) +
                                                        " needs to be initialized before it can be terminated.");
                }

                if (_keyboardHook != null)
                {
                    _keyboardHook.Dispose();
                    _keyboardHook = null;
                }

                if (_mouseHook != null)
                {
                    _mouseHook.Dispose();
                    _mouseHook = null;
                }

                OnKeyboardEvent = null;
                OnMouseEvent    = null;

                _keyStateChangedCallbacks = null;
                _keyStates = null;

                IsInitialized = false;
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Initializes the specified capture mouse move.
        /// </summary>
        /// <param name="captureMouseMove">if set to <c>true</c> [capture mouse move].</param>
        /// <param name="clearInjectedFlag">if set to <c>true</c> [clear injected flag].</param>
        /// <exception cref="InvalidOperationException">The " + nameof(InputManager) + " is already initialized.</exception>
        public void Initialize(bool captureMouseMove, bool clearInjectedFlag)
        {
            lock (_lockObject)
            {
                if (IsInitialized)
                {
                    throw new InvalidOperationException("The " + nameof(InputManager) + " is already initialized.");
                }

                _keyStateChangedCallbacks = new Dictionary <VirtualKeyCode, List <KeyStateChangedEventHandler> >();
                _keyStates = new Dictionary <VirtualKeyCode, KeyState>();

                foreach (KeyValuePair <VirtualKeyCode, string> pair in KeyCodeConverter.EnumerateVirtualKeyCodes())
                {
                    _keyStateChangedCallbacks.Add(pair.Key, new List <KeyStateChangedEventHandler>());
                    _keyStates.Add(pair.Key, KeyState.None);
                }

                _keyboardHook = new LowLevelKeyboardHook(clearInjectedFlag);
                _mouseHook    = new LowLevelMouseHook(captureMouseMove, clearInjectedFlag);

                _keyboardHook.OnKeyboardEvent += _keyboardHook_OnKeyboardEvent;
                _mouseHook.OnMouseEvent       += _mouseHook_OnMouseEvent;

                _keyboardHook.InstallHook();
                _mouseHook.InstallHook();

                IsInitialized = true;
            }
        }