예제 #1
0
        protected override void OnMessageReceived(object sender, MessageEventArgs e)
        {
            Contract.Requires(e != null);

            var eventArgs = new MouseMessageEventArgs
            {
                MessageCode = BitConverter.ToInt32(e.Bytes, 0),
                X           = BitConverter.ToInt32(e.Bytes, 4),
                Y           = BitConverter.ToInt32(e.Bytes, 8),
                Handle      = BitConverter.ToInt32(e.Bytes, 12),
                HitTestCode = BitConverter.ToInt32(e.Bytes, 16),
                Delta       = BitConverter.ToInt16(e.Bytes, 20),
            };

            Debug.WriteLine($"Code: {eventArgs.MessageCode}; X: {eventArgs.X}; Y: {eventArgs.Y}; Delta: {eventArgs.Delta}");

            MessageReceived?.Invoke(this, eventArgs);

            switch (GetMessageCode(eventArgs.MessageCode))
            {
            case MouseMessageCode.MouseMove:
                MouseMove?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDown:
                LeftButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonUp:
                LeftButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDblClk:
                LeftButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDown:
                RightButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonUp:
                RightButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDblClk:
                RightButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDown:
                MiddleButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonUp:
                MiddleButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDblClk:
                MiddleButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseWheel:
                MouseWheel?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDown:
                XButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonUp:
                XButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDblClk:
                XButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseHWheel:
                MouseHWheel?.Invoke(this, eventArgs);
                break;

            default:
                break;
            }

            if (_messageHandlers.ContainsKey(eventArgs.MessageCode))
            {
                _messageHandlers[eventArgs.MessageCode]?.Invoke(this, eventArgs);
            }
        }
예제 #2
0
파일: MouseHook.cs 프로젝트: macote/Winook
        protected override void OnMessageReceived(object sender, MessageEventArgs e)
        {
            Contract.Requires(e != null);

            var modifiers = BitConverter.ToUInt16(e.Bytes, 24);
            var eventArgs = new MouseMessageEventArgs
            {
                MessageCode = BitConverter.ToInt32(e.Bytes, 0),
                X           = BitConverter.ToInt32(e.Bytes, 4),
                Y           = BitConverter.ToInt32(e.Bytes, 8),
                Handle      = BitConverter.ToInt32(e.Bytes, 12),
                HitTestCode = BitConverter.ToInt32(e.Bytes, 16),
                Modifiers   = modifiers,
                Shift       = (modifiers & 0b100) > 0,
                Control     = (modifiers & 0b10) > 0,
                Alt         = (modifiers & 0b1) > 0,
            };

            var messageCode = GetMessageCode(eventArgs.MessageCode);

            if (messageCode == MouseMessageCode.MouseWheel)
            {
                eventArgs.Delta = BitConverter.ToInt16(e.Bytes, 20);
            }
            else if (messageCode == MouseMessageCode.NCXButtonDown ||
                     messageCode == MouseMessageCode.NCXButtonUp ||
                     messageCode == MouseMessageCode.NCXButtonDblClk ||
                     messageCode == MouseMessageCode.XButtonDown ||
                     messageCode == MouseMessageCode.XButtonUp ||
                     messageCode == MouseMessageCode.XButtonDblClk)
            {
                eventArgs.XButtons = BitConverter.ToInt16(e.Bytes, 20);
            }

            Debug.Write($"Code: {eventArgs.MessageCode}; X: {eventArgs.X}; Y: {eventArgs.Y}; Modifiers: {eventArgs.Modifiers:x}; ");
            Debug.WriteLine($"Delta: {eventArgs.Delta}; XButtons: {eventArgs.XButtons}");

            MessageReceived?.Invoke(this, eventArgs);

            switch (messageCode)
            {
            case MouseMessageCode.MouseMove:
                MouseMove?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDown:
                LeftButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonUp:
                LeftButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDblClk:
                LeftButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDown:
                RightButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonUp:
                RightButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDblClk:
                RightButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDown:
                MiddleButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonUp:
                MiddleButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDblClk:
                MiddleButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseWheel:
                MouseWheel?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDown:
                XButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonUp:
                XButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDblClk:
                XButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseHWheel:
                MouseHWheel?.Invoke(this, eventArgs);
                break;

            default:
                break;
            }

            if (_messageHandlers.ContainsKey(eventArgs.MessageCode))
            {
                _messageHandlers[eventArgs.MessageCode]?.Invoke(this, eventArgs);
            }
        }