C
C#ā€¢2y ago
med5801

ā” Need Help

using System;
using System.Diagnostics;
using System.IO.Ports;

namespace ABRA_KEYBOARD_CTRL
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press ESC to stop");
SerialPort port = new SerialPort("COM5", 115200);
port.DataBits = 8;

port.StopBits = StopBits.One;
port.Handshake = Handshake.None;
port.Parity = Parity.None;
port.Open();
port.Write("#Keyboard App Started#");
do {
while (!Console.KeyAvailable) ;
{
switch (Console.ReadKey(true).Key)
{

default:
Console.WriteLine("ERR!");
break;
case ConsoleKey.W:
Console.WriteLine("KeyPressed: W");
port.Write("1");
break;
case ConsoleKey.D:
Console.WriteLine("KeyPressed: D");
port.Write("D");
break;
case ConsoleKey.UpArrow:
Console.WriteLine("KeyPressed: up");
port.Write("3");
break;
case ConsoleKey.Escape:
port.Close();
Environment.Exit(0);
break;
}
}
}

while (Console.ReadKey(true).Key != ConsoleKey.Escape);

Process.GetCurrentProcess().WaitForExit();
}
}
}
using System;
using System.Diagnostics;
using System.IO.Ports;

namespace ABRA_KEYBOARD_CTRL
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press ESC to stop");
SerialPort port = new SerialPort("COM5", 115200);
port.DataBits = 8;

port.StopBits = StopBits.One;
port.Handshake = Handshake.None;
port.Parity = Parity.None;
port.Open();
port.Write("#Keyboard App Started#");
do {
while (!Console.KeyAvailable) ;
{
switch (Console.ReadKey(true).Key)
{

default:
Console.WriteLine("ERR!");
break;
case ConsoleKey.W:
Console.WriteLine("KeyPressed: W");
port.Write("1");
break;
case ConsoleKey.D:
Console.WriteLine("KeyPressed: D");
port.Write("D");
break;
case ConsoleKey.UpArrow:
Console.WriteLine("KeyPressed: up");
port.Write("3");
break;
case ConsoleKey.Escape:
port.Close();
Environment.Exit(0);
break;
}
}
}

while (Console.ReadKey(true).Key != ConsoleKey.Escape);

Process.GetCurrentProcess().WaitForExit();
}
}
}
I need to send a message when when i release any keyboard button. How can i do that?
20 Replies
med5801
med5801OPā€¢2y ago
this code will run a submarine drone. It should go forward when I press the key and stop when I release the key. Thanks for your help
TheRanger
TheRangerā€¢2y ago
you realize it will keep spamming writing "1" to the port if u keep holding W, right?
med5801
med5801OPā€¢2y ago
yeah
TheRanger
TheRangerā€¢2y ago
so if you stop holding the key, it wont write anymore does the submarine drone still go forward after u release the key?
med5801
med5801OPā€¢2y ago
Actually "1" is running another function
TheRanger
TheRangerā€¢2y ago
my point is, does it still run the function after u release the key?
med5801
med5801OPā€¢2y ago
yes there is a second code
if(Serial.available()){
a=(int)Serial.read();
Serial.println(a);
if(a==49){
on=on+potValue;
ESC1.write(on);

Serial.println("ilerle = "+String(on));

}
if(Serial.available()){
a=(int)Serial.read();
Serial.println(a);
if(a==49){
on=on+potValue;
ESC1.write(on);

Serial.println("ilerle = "+String(on));

}
TheRanger
TheRangerā€¢2y ago
well you might need to create an InputManager class
med5801
med5801OPā€¢2y ago
okey i will try thanks
TheRanger
TheRangerā€¢2y ago
i can help you how to do it
med5801
med5801OPā€¢2y ago
it would be great is there any function to get release key input
TheRanger
TheRangerā€¢2y ago
afaik for console, no you'd have to make one
med5801
med5801OPā€¢2y ago
i need to google it
TheRanger
TheRangerā€¢2y ago
when you press W for example you will store a data that W is being held
med5801
med5801OPā€¢2y ago
okey after that?
TheRanger
TheRangerā€¢2y ago
and when you release it, the input class will check what keys are currently pressed, and what keys last updated were currently pressed if you are not holding W but last time you updated, u were holding W then that indicates that u just released the key W
med5801
med5801OPā€¢2y ago
oh i see i will try thanks for your help šŸ‘
TheRanger
TheRangerā€¢2y ago
@Med5801 just discovered its very hard to do on Console due to limitations u might need to use a keyboard hook instead https://stackoverflow.com/questions/52903672/wait-for-key-to-be-released
Stack Overflow
Wait for key to be released
In a simple test program that i made, you can increment a value with Console.ReadKey(). However, you can hold the button, and it will just keep increasing the value. I want my program to wait for t...
TheRanger
TheRangerā€¢2y ago
and dont try to close the thread as someone might have an answer better than mine
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server