This is the code I use to send a key code
using System.IO.Ports;
using System;
using System.Threading;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
namespace Arduino3
{
internal static class Program {
//This is just a way to communicate with an Arduino
static SerialPort _serialPort;
[STAThread]
static void Main()
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM5";//Set your board COM
_serialPort.BaudRate = 9600;
_serialPort.Open();
while (true)
{
string a = _serialPort.ReadExisting();
Console.WriteLine(a);
if(a == "test")
{
SendKey();
}
Thread.Sleep(500);
}
}
//This is one way I tried to send keys
private static void SendKey()
{
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
var p = Process.GetProcessesByName("Discord").LastOrDefault();
var pointer = p.Handle;
SetForegroundWindow(pointer);
SendKeys.SendWait("F13");//F13,14,15,16
SendKeys.Flush();
}
}
}
I can send keys like F13, F14, F15, F16, but F17 to F24 doesn't work.
And for keys D0-D9 I know nothing about and couldn't find much to answer my questions.
I tried using it like {F17},{D0} but it outputs an error saying "Keyword "D0" is not valid".
CodePudding user response: