SebastianLawe

Untitled

May 16th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MouseKeyboardActivityMonitor;
  6. using System.Windows.Forms;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9. using System.Net;
  10. using System.IO;
  11. using WindowsInput;
  12. using Interceptor;
  13.  
  14. namespace UniversalMacro {
  15.     public class UdpInputDevice : GenericInputDevice {
  16.         string _ipAddress;
  17.         public string IpAddress {
  18.             get {
  19.                 return _ipAddress;
  20.             }
  21.             set {
  22.                 _ipAddress = value;
  23.             }
  24.         }
  25.         public UdpInputDevice(string ipAddress, string name) {
  26.             _ipAddress = ipAddress;
  27.             Name = name;
  28.         }
  29.         public override void CreateDeviceThread(KeyboardHookListener keyboardListener, Button learnButton, Button macroButton) {
  30.             Thread thread = new Thread(new ThreadStart(delegate {
  31.                 Input input = new Input();
  32.                 input.Load();
  33.                 input.KeyboardFilterMode = KeyboardFilterMode.All;
  34.                 IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 18000);
  35.                 UdpClient client = new UdpClient(ipEndPoint);
  36.                 while (true) {
  37.                     using (MemoryStream inputStream = new MemoryStream(client.Receive(ref ipEndPoint))) {
  38.                         using (StreamReader reader = new StreamReader(inputStream)) {
  39.                             string ipAddress = reader.ReadLine();
  40.                             if (ipAddress == _ipAddress) {
  41.                                 string buttonAffected = reader.ReadLine();
  42.                                 string buttonAction = reader.ReadLine();
  43.                                 int keyIndex = 0;
  44.                                 if (!LearnKey && !LearnMacro) {
  45.                                     for (int i = 0; i < KeyBindings.Count; i++) {
  46.                                         if ((KeyBindings[i].Key as string) == buttonAffected) {
  47.                                             keyIndex = i;
  48.                                             CurrentKey = KeyBindings[i].Value;
  49.                                             break;
  50.                                         } else {
  51.                                             CurrentKey = null;
  52.                                         }
  53.                                     }
  54.                                     switch (buttonAction) {
  55.                                         case "[down]":
  56.                                             InputSimulator.SimulateKeyDown(TranslateToVirtualKeyCode(CurrentKey));
  57.                                             input.SendKey(TranslateToInterceptorKey(CurrentKey), Interceptor.KeyState.Down);
  58.                                             break;
  59.                                         case "[up]":
  60.                                             InputSimulator.SimulateKeyUp(TranslateToVirtualKeyCode(CurrentKey));
  61.                                             input.SendKey(TranslateToInterceptorKey(CurrentKey), Interceptor.KeyState.Up);
  62.                                             break;
  63.                                     }
  64.                                 } else if (LearnedButton == null && LearnKey) {
  65.                                     learnButton.Invoke(new MethodInvoker(delegate {
  66.                                         LearnedButton = buttonAffected;
  67.                                         learnButton.Text = "Press Keyboard Button";
  68.                                     }));
  69.                                 } else if (LearnMacro) {
  70.                                     LearnMacro = false;
  71.                                     NamingDialogue dialogue = new NamingDialogue();
  72.                                     if (dialogue.ShowDialog() == DialogResult.OK) {
  73.                                         KeyBindings.Add(new KeyValuePair<object, string>(buttonAffected, dialogue.StringEntered + ".macro"));
  74.                                         macroButton.Invoke(new MethodInvoker(delegate {
  75.                                             macroButton.Text = "Bind To Macro";
  76.                                             macroButton.Enabled = true;
  77.                                             macroButton.Parent.Enabled = true;
  78.                                         }));
  79.                                     }
  80.                                 }
  81.                             }
  82.                         }
  83.                     }
  84.                 }
  85.             }));
  86.             thread.Start();
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment