Ardujoy.cs

C# jimmyd 8 Views Size: 2.60 KB Posted on: Oct 5, 25 @ 11:03 AM
  1. 1using System;
  2. 2using System.IO.Ports;
  3. 3using System.Runtime.InteropServices;
  4. 4
  5. 5namespace ardujoy
  6. 6{
  7. 7 class Program
  8. 8 {
  9. 9 [DllImport("user32.dll")]
  10. 10 private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
  11. 11
  12. 12 const uint KEYEVENTF_KEYUP = 0x0002;
  13. 13
  14. 14 public static void Main(string[] args)
  15. 15 {
  16. 16 string line;
  17. 17 string JOY_BUTTON="1";
  18. 18 string JOY_UP="1";
  19. 19 string JOY_DOWN="1";
  20. 20 string JOY_LEFT="1";
  21. 21 string JOY_RIGHT="1";
  22. 22
  23. 23 string PREV_BUTTON="1";
  24. 24 string PREV_UP="1";
  25. 25 string PREV_DOWN="1";
  26. 26 string PREV_LEFT="1";
  27. 27 string PREV_RIGHT="1";
  28. 28
  29. 29 char KEY_BUTTON='Q';
  30. 30 char KEY_UP='W';
  31. 31 char KEY_DOWN='S';
  32. 32 char KEY_LEFT='A';
  33. 33 char KEY_RIGHT='D';
  34. 34
  35. 35 if (args.Length == 1) {
  36. 36 var port = new SerialPort(args[0],115200, Parity.None, 8, StopBits.One);
  37. 37
  38. 38 port.Open();
  39. 39 port.DiscardOutBuffer();
  40. 40 port.DiscardInBuffer();
  41. 41 while (port.IsOpen) {
  42. 42 line = port.ReadLine();
  43. 43// Console.WriteLine(line);
  44. 44 if (line.Length == 5) {
  45. 45 JOY_BUTTON=line.Substring(0,1);
  46. 46 JOY_UP=line.Substring(4,1);
  47. 47 JOY_DOWN=line.Substring(3,1);
  48. 48 JOY_LEFT=line.Substring(2,1);
  49. 49 JOY_RIGHT=line.Substring(1,1);
  50. 50
  51. 51 if (JOY_BUTTON != PREV_BUTTON) {
  52. 52 PREV_BUTTON=JOY_BUTTON;
  53. 53 if (JOY_BUTTON == "0") {
  54. 54 keybd_event(Convert.ToByte(KEY_BUTTON), 0, 0, 0);
  55. 55 } else {
  56. 56 keybd_event(Convert.ToByte(KEY_BUTTON), 0, KEYEVENTF_KEYUP, 0);
  57. 57 }
  58. 58 }
  59. 59 if (JOY_UP != PREV_UP) {
  60. 60 PREV_UP=JOY_UP;
  61. 61 if (JOY_UP == "0") {
  62. 62 keybd_event(Convert.ToByte(KEY_UP), 0, 0, 0);
  63. 63 } else {
  64. 64 keybd_event(Convert.ToByte(KEY_UP), 0, KEYEVENTF_KEYUP, 0);
  65. 65 }
  66. 66 }
  67. 67 if (JOY_DOWN != PREV_DOWN) {
  68. 68 PREV_DOWN=JOY_DOWN;
  69. 69 if (JOY_DOWN == "0") {
  70. 70 keybd_event(Convert.ToByte(KEY_DOWN), 0, 0, 0);
  71. 71 } else {
  72. 72 keybd_event(Convert.ToByte(KEY_DOWN), 0, KEYEVENTF_KEYUP, 0);
  73. 73 }
  74. 74 }
  75. 75 if (JOY_LEFT != PREV_LEFT) {
  76. 76 PREV_LEFT=JOY_LEFT;
  77. 77 if (JOY_LEFT == "0") {
  78. 78 keybd_event(Convert.ToByte(KEY_LEFT), 0, 0, 0);
  79. 79 } else {
  80. 80 keybd_event(Convert.ToByte(KEY_LEFT), 0, KEYEVENTF_KEYUP, 0);
  81. 81 }
  82. 82 }
  83. 83 if (JOY_RIGHT != PREV_RIGHT) {
  84. 84 PREV_RIGHT=JOY_RIGHT;
  85. 85 if (JOY_RIGHT == "0") {
  86. 86 keybd_event(Convert.ToByte(KEY_RIGHT), 0, 0, 0);
  87. 87 } else {
  88. 88 keybd_event(Convert.ToByte(KEY_RIGHT), 0, KEYEVENTF_KEYUP, 0);
  89. 89 }
  90. 90 }
  91. 91 }
  92. 92 }
  93. 93 } else Console.WriteLine("You must provide the name of the serial port to use.");
  94. 94 }
  95. 95 }
  96. 96}

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA), and — with your consent — to measure usage and show ads. See Privacy.