One evening, a son came up to me and said that he wanted to play Mario. In the summer at his grandmother's cottage, he loved to “wriggle out” in rainy weather. And it was raining outside. Without hesitation, I downloaded him the first emulator of an 8-bit set-top box and the game. However, it turned out that the pleasure of playing the keyboard is not at all the same. Going to buy a joystick was too late. And then I thought that you can do without it. At hand we had an old Nokia Lumia, its size and shape roughly coincided with our needs. It was decided to write a joystick. The son went to draw a design on a piece of paper in the cell, and dad went to make coffee and think about how to implement this idea with the least amount of time.
using System.Net; using System.Net.Sockets; public partial class ServerForm : Form { private Socket _serverSocket, _clientSocket; private byte[] _buffer; private void StartServer() { try { _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 3333)); _serverSocket.Listen(0); _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } public ServerForm() { InitializeComponent(); StartServer(); } [ DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); private const int KEYEVENTF_EXTENDEDKEY = 1; private const int KEYEVENTF_KEYUP = 2; public static void KeyDown(Keys vKey) { keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0); } public static void KeyUp(Keys vKey) { keybd_event((byte)vKey, 0, KEYEVENTF_KEYUP, 0); } private void ReceiveCallback(IAsyncResult AR) { try { int received = _clientSocket.EndReceive(AR); Array.Resize(ref _buffer, received); string text = Encoding.ASCII.GetString(_buffer); // AppendToTextBox(text); // ------------------ Array.Resize(ref _buffer, _clientSocket.ReceiveBufferSize); _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void AppendToTextBox(string text) { MethodInvoker invoker = new MethodInvoker(delegate { string text_before = text; string exitW = text; // if (text == "a") { KeyUp(Keys.D); KeyDown(Keys.A); textBox.Text += text + " "; } // if (text == "a1" ) { KeyUp(Keys.A); textBox.Text += text + " "; } }); this.Invoke(invoker); } if (text == "a") { KeyUp(Keys.D); KeyDown(Keys.A); textBox.Text += text + " "; } 
using Windows.Networking.Sockets; using Windows.Networking; using Windows.Storage.Streams; StreamSocket clientSocket = new StreamSocket(); private async void btnConnect_Click(object sender, RoutedEventArgs e) { HostName host = new HostName(textBoxIP.Text); string port = textBoxPort.Text; if (connected) { StatusText.Text = " "; return; } try { StatusText.Text = " ..."; await clientSocket.ConnectAsync(host, port); connected = true; StatusText.Text = " " + Environment.NewLine; } catch (Exception exception) { if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } StatusText.Text = " : "; closing = true; clientSocket.Dispose(); clientSocket = null; } } private async void sendkey(string key) { if (!connected) { StatusText.Text = " "; return; } try { StatusText.Text = " ..."; DataWriter writer = new DataWriter(clientSocket.OutputStream); writer.WriteString(key); await writer.StoreAsync(); StatusText.Text = " " + Environment.NewLine; writer.DetachStream(); writer.Dispose(); } catch (Exception exception) { if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } StatusText.Text = " "; closing = true; clientSocket.Dispose(); clientSocket = null; connected = false; } } // // private void gotFocusUp(object sender, RoutedEventArgs e) { sendkey("w"); } private void lostFocusUp(object sender, RoutedEventArgs e) { sendkey("w1"); btnConnect.Focus(FocusState.Programmatic); } private void lostFocusUp(object sender, PointerRoutedEventArgs e) { sendkey("w1"); btnConnect.Focus(FocusState.Programmatic); } 
Source: https://habr.com/ru/post/274271/
All Articles