Beispiel #1
0
        /**
         * On keyboard event.
         */
        private void GlobalHookKeyDown(object sender, KeyEventArgs e)
        {
            Keys key = e.KeyCode;

            if (isEditing)
            {
                RecordKey editedKey = keyDict.First(k => k.clientKey.ToString() == editingClientKey);
                editedKey.serverKey = key;
                isEditing           = false;
                fillList();
            }
            else if (adding)
            {
                if (keyDict.Any(k => k.clientKey == key))
                {
                    keyDict.RemoveAll(k => k.clientKey == key);
                }
                else
                {
                    RecordKey k = new RecordKey(key, key);
                    Console.WriteLine(k.getCode());


                    keyDict.Add(new RecordKey(key, key));
                }
                fillList();
            }
            else if (addingSpecial)
            {
                ItemSelector selector = new ItemSelector();
                var          result   = selector.ShowDialog();
                if (result == DialogResult.OK)
                {
                    ItemState resultState = selector.ItemState;
                    keyDict.Add(new RecordKey(key, resultState));
                }
                fillList();
                addingSpecial      = false;
                btnAddSpecial.Text = "Item";
            }
            else if (keyDict.Any(k => k.clientKey == key) && clientConnected)
            {
                // Assume we can have only one
                RecordKey recordKey = keyDict.First(k => k.clientKey == key);
                try
                {
                    client = new TcpClient(txtClientAddress.Text, clientPort);
                    using (NetworkStream stream = client.GetStream())
                    {
                        Byte[] data = BitConverter.GetBytes(recordKey.getCode());
                        stream.Write(data, 0, data.Length);
                    }
                    client.Close();
                }
                catch (SocketException ex)
                {
                    Console.WriteLine("SocketException: {0}", ex);
                    clientStop();
                }
            }
        }
Beispiel #2
0
        /**
         * Handle incoming connection on listener socket.
         */
        private async void HandleAsyncConnection(IAsyncResult res)
        {
            if (serverRunning)
            {
                // Listen for upcoming connection.
                initListen();
            }
            else
            {
                // Or cancel if sever is stopped.
                return;
            }

            // Socket for connected client
            receiveSocket = listener.EndAcceptTcpClient(res);
            StringBuilder sb = new StringBuilder();

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            using (NetworkStream networkStream = receiveSocket.GetStream())
            {
                var buffer = new byte[10];
                await networkStream.ReadAsync(buffer, 0, buffer.Length, cancellationTokenSource.Token);

                // Read key from network stream
                int keyValue = BitConverter.ToInt32(buffer, 0);

                if (ItemState.isValidCode(keyValue))
                {
                    ItemState newState = new ItemState(keyValue);
                    DCItemState.setState(newState);
                }
                else
                {
                    // Convert to VirtualKeyCode
                    VirtualKeyCode key = (VirtualKeyCode)keyValue;

                    // Simulate a keypress on the client
                    switch (key)
                    {
                    /* Zoom in/out can be done using keypress (smallest step) */
                    case VirtualKeyCode.F1:
                    case VirtualKeyCode.F2:
                        simulator.Keyboard.KeyPress(key);
                        break;

                    /* Icon size can be done using 10 ms keydown (will not register otherwise) */
                    case VirtualKeyCode.F3:
                    case VirtualKeyCode.F4:
                        simulator.Keyboard.KeyDown(key);
                        Thread.Sleep(10);
                        simulator.Keyboard.KeyUp(key);
                        break;

                    /* Boolean settings can be done using 50 ms (will not register otherwise) */
                    default:
                        simulator.Keyboard.KeyDown(key);
                        Thread.Sleep(50);
                        simulator.Keyboard.KeyUp(key);
                        break;
                    }
                }
                receiveSocket.Close();
            }
        }