Beispiel #1
0
 private IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
 {
     if (nCode >= 0)
     {
         var st = Marshal.PtrToStructure <KeyboardLowLevelHookStruct>(lParam);
         OnKeyboardKeyPress?.Invoke(this, new KeyboardInputEventArgs(st.vkCode, (KeyboardHookHelper.KeyboardMessage)wParam));
     }
     return(KeyboardHookHelper.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
 }
Beispiel #2
0
        private void Udp_OnMessageReceived(object sender, string message)
        {
            //message format:     [COMMAND_ID:DATA:TARGET_NAME]

            //is message valid
            if (!message.StartsWith("[") || !message.EndsWith("]"))//message isn't boxed properly, this probably wasn't even meant for us, likely just a beacon echo
            {
                //OnError?.Invoke(this, new IOException($"message wasn't boxed properly: {message}"));
                return;
            }

            OnMouseSignalReceived?.Invoke(this, message);

            var commandParts = message.Trim('[', ']').Split(':');

            if (commandParts.Length != 3)//message had too many parts
            {
                OnError?.Invoke(this, new IOException($"message is not internally formatted correctly, should be 3 parts: {message}"));
                return;
            }

            var commandId = 0;

            if (!int.TryParse(commandParts[0], out commandId))
            {
                OnError?.Invoke(this, new IOException($"invalid commandId string, must be an integer value, received {commandParts[0]}"));
                return;
            }

            //route message
            switch (commandId)
            {
            case PhoneRemoteCommands.MOUSE_POSITION_MOVE:
            {
                //format of x,y
                var dataParts = commandParts[1].Split(',');
                var x         = 0;
                var y         = 0;
                if (dataParts.Length != 2 || !int.TryParse(dataParts[0], out x) || !int.TryParse(dataParts[1], out y))
                {
                    OnError?.Invoke(this, new IOException($"move command data invalid: {commandParts[1]} in message {message}"));
                }

                if (x == 0 && y == 0)         //not an error, just not useful
                {
                    return;
                }

                OnMouseMove?.Invoke(this, new Point(x, y));
                break;
            }

            case PhoneRemoteCommands.MOUSE_POSITION_SET:
            {
                var dataParts = commandParts[1].Split(',');
                var x         = 0;
                var y         = 0;
                if (dataParts.Length != 2 || !int.TryParse(dataParts[0], out x) || !int.TryParse(dataParts[1], out y))
                {
                    OnError?.Invoke(this, new IOException($"set command data invalid: {commandParts[1]} in message {message}"));
                }

                OnMouseSet?.Invoke(this, new Point(x, y));
                break;
            }

            case PhoneRemoteCommands.MOUSE_LEFT_CLICK:
                OnMouseLeftClick?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_LEFT_DOUBLE_CLICK:
                OnMouseDoubleLeftClick?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_LEFT_PRESS:
                OnMouseLeftPress?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_LEFT_RELEASE:
                OnMouseLeftRelease?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_RIGHT_CLICK:
                OnMouseRightClick?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_RIGHT_DOUBLE_CLICK:
                OnMouseDoubleRightClick?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_RIGHT_PRESS:
                OnMouseRightPress?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_RIGHT_RELEASE:
                OnMouseRightRelease?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_SCROLLWHEEL_CLICK:
                OnScrollwheelClick?.Invoke(this, EventArgs.Empty);
                break;

            case PhoneRemoteCommands.MOUSE_SCROLLWHEEL_SCROLL_MOVE:
            {
                var moveAmount = 0;
                if (!int.TryParse(commandParts[1], out moveAmount))
                {
                    OnError?.Invoke(this, new IOException($"mouse scroll data invalid: {commandParts[1]}, message: {message}"));
                    return;
                }

                OnScrollwheelMove?.Invoke(this, moveAmount);
                break;
            }

            case PhoneRemoteCommands.KEYBOARD_KEY_CLICK:
            {
                if (string.IsNullOrWhiteSpace(commandParts[1]))
                {
                    OnError?.Invoke(this, new IOException($"keyboard click data missing, message: {message}"));
                    return;
                }

                OnKeyboardKeyClick?.Invoke(this, commandParts[1]);
                break;
            }

            case PhoneRemoteCommands.KEYBOARD_KEY_PRESS:
            {
                if (string.IsNullOrWhiteSpace(commandParts[1]))
                {
                    OnError?.Invoke(this, new IOException($"keyboard press data missing, message: {message}"));
                    return;
                }

                OnKeyboardKeyPress?.Invoke(this, commandParts[1]);
                break;
            }

            case PhoneRemoteCommands.KEYBOARD_KEY_RELEASE:
            {
                if (string.IsNullOrWhiteSpace(commandParts[1]))
                {
                    OnError?.Invoke(this, new IOException($"keyboard release data missing, message: {message}"));
                    return;
                }

                OnKeyboardKeyRelease?.Invoke(this, commandParts[1]);
                break;
            }

            case PhoneRemoteCommands.KEYBOARD_KEY_STRING:
            {
                if (string.IsNullOrWhiteSpace(commandParts[1]))
                {
                    OnError?.Invoke(this, new IOException($"keyboard string data missing, message: {message}"));
                    return;
                }

                OnKeyboardKeyString?.Invoke(this, commandParts[1]);
                break;
            }

            case PhoneRemoteCommands.INVALID_COMMAND:
            default:
            {
                OnError?.Invoke(this, new IOException($"invalid commandId, no command associated with ID: {commandId}, message: {message}"));
                return;
            }
            }
        }