using Gma.System.MouseKeyHook; using NLog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace POSV.Utils { public class KeyboardUtils { private Logger logger = NLog.LogManager.GetCurrentClassLogger(); private IKeyboardMouseEvents _events; private static object _lock = new object(); private static KeyboardUtils _instance = null; private KeyboardUtils() { this._events = Hook.AppEvents(); } public static KeyboardUtils Instance { get { if (_instance == null) { lock (_lock) { _instance = new KeyboardUtils(); } } return _instance; } } public void RegisterHook(KeyEventHandler appHookKeyDown) { this.RegisterHook(appHookKeyDown,"注册键盘事件"); } public void RegisterHook(KeyEventHandler appHookKeyDown,string message) { this._events.KeyDown += appHookKeyDown; logger.Debug(message); } public void UnRegisterHook(KeyEventHandler appHookKeyDown) { this.UnRegisterHook(appHookKeyDown , "卸载键盘事件"); } public void UnRegisterHook(KeyEventHandler appHookKeyDown , string message) { this._events.KeyDown -= appHookKeyDown; logger.Debug(message); } } }