C
C#2y ago
Elmishh

❔ making a consoleapp stop when pressing a key

how do i make a ConsoleApp stop when pressing WHILE running, i dont want it to stop i want the program to continue working until a chosen key is pressed
21 Replies
Elmishh
ElmishhOP2y ago
i have a program that moves my mouse every few seconds and i want a failsafe in case it doesnt stop when its supposed to
Jester
Jester2y ago
when you press a chosen key in the console or just anywhere?
Jester
Jester2y ago
Stack Overflow
Non-Blocking read from standard I/O in C#
I want a non-blocking read function from console. How do I write that in C#?
Jester
Jester2y ago
or use a hotkey if you want to be able to stop it even when the console isnt the active window (windows only, you need to dll import it)
Jester
Jester2y ago
first you register a hotkey, and then in the loop of your application you can use PeekMessage to see if the hotkey has been pressed
Elmishh
ElmishhOP2y ago
that shows C++ code but i want to be able to just stop even while console isnt active
Monsieur Wholesome
You can run your funky code concurrently via a Task, and in the main Program you do a lil loop Actually, no that requires you to have the console open in front of you which thinking about your use case of moving the mouse around wildly, you will likely not have
Monsieur Wholesome
Checkout Mouse + Keyboard Hook / "Keylogger" https://github.com/TolikPylypchuk/SharpHook
GitHub
GitHub - TolikPylypchuk/SharpHook: SharpHook provides a cross-platf...
SharpHook provides a cross-platform global keyboard and mouse hook for .NET, and the ability to simulate input events - GitHub - TolikPylypchuk/SharpHook: SharpHook provides a cross-platform global...
Jester
Jester2y ago
maybe a bit overkill but a nice abstraction
Monsieur Wholesome
How else will you establish a global keylogger? Is it that easy? Enlighten me
Jester
Jester2y ago
well if its just 1 key to stop the application, a hotkey is easier to do
Monsieur Wholesome
Wouldnt dare messing with P/Invoke and prefer a C# lib "just for a hotkey"
Jester
Jester2y ago
another way to do a global keylogger is instead of creating a hook is using raw input here, i wrote this example of how you could use a hotkey (windows only)
using System;
using System.Runtime.InteropServices;
using System.Threading;

const int MOD_CONTROL = 0x2;
const int MOD_NOREPEAT = 0x4000;
const int PM_REMOVE = 0x1;
const int WM_HOTKEY = 0x0312;

// register hotkey ctrl+p
RegisterHotKey(
IntPtr.Zero,
1,
MOD_CONTROL | MOD_NOREPEAT,
'P'
);

while (true)
{
// the logic of your program
Console.WriteLine("Hello!");
Thread.Sleep(1000);

// check if there is a message available
if (PeekMessage(out Msg msg, IntPtr.Zero, 0, 0, PM_REMOVE) > 0)
{
if (msg.message == WM_HOTKEY) // there was a message for to tell us the hotkey was pressed!
{
break; // we can stop now
}
}
}

return;

// some stuff to use the C functions from windows in our C#

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
[DllImport("user32")]
static extern int RegisterHotKey(
IntPtr hWnd,
int id,
uint fsModifiers,
uint vk
);

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagew
[DllImport("user32", CharSet = CharSet.Unicode, EntryPoint = "PeekMessageW")]
static extern int PeekMessage(
out Msg lpMsg,
IntPtr hWnd,
uint wMsgFilterMin,
uint wMsgFilterMax,
uint wRemoveMsg
);

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msg
[StructLayout(LayoutKind.Sequential)]
struct Msg
{
public IntPtr hwnd;
public uint message;
public nuint wParam;
public nuint lParam;
public uint time;
public int ptx;
public int pty;
public uint lPrivate;
}
using System;
using System.Runtime.InteropServices;
using System.Threading;

const int MOD_CONTROL = 0x2;
const int MOD_NOREPEAT = 0x4000;
const int PM_REMOVE = 0x1;
const int WM_HOTKEY = 0x0312;

// register hotkey ctrl+p
RegisterHotKey(
IntPtr.Zero,
1,
MOD_CONTROL | MOD_NOREPEAT,
'P'
);

while (true)
{
// the logic of your program
Console.WriteLine("Hello!");
Thread.Sleep(1000);

// check if there is a message available
if (PeekMessage(out Msg msg, IntPtr.Zero, 0, 0, PM_REMOVE) > 0)
{
if (msg.message == WM_HOTKEY) // there was a message for to tell us the hotkey was pressed!
{
break; // we can stop now
}
}
}

return;

// some stuff to use the C functions from windows in our C#

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
[DllImport("user32")]
static extern int RegisterHotKey(
IntPtr hWnd,
int id,
uint fsModifiers,
uint vk
);

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagew
[DllImport("user32", CharSet = CharSet.Unicode, EntryPoint = "PeekMessageW")]
static extern int PeekMessage(
out Msg lpMsg,
IntPtr hWnd,
uint wMsgFilterMin,
uint wMsgFilterMax,
uint wRemoveMsg
);

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msg
[StructLayout(LayoutKind.Sequential)]
struct Msg
{
public IntPtr hwnd;
public uint message;
public nuint wParam;
public nuint lParam;
public uint time;
public int ptx;
public int pty;
public uint lPrivate;
}
it can look intimidating, but its pretty simple, as you can see in the loop
Monsieur Wholesome
inb4 youre going to bluescreen using that kek
Jester
Jester2y ago
there is no way this is much more than this simple hotkey https://github.com/kwhat/libuiohook/blob/1.2/src/windows/input_hook.c
Elmishh
ElmishhOP2y ago
Id rather have the code for a hotkey be shorter than my actual code
Jester
Jester2y ago
hide it in another file goodthinking
Elmishh
ElmishhOP2y ago
?
Jester
Jester2y ago
namespaces, classes, functions is just hiding, organizing and reusing code, this hotkey stuff looks like a lot bcuz its not hidden away
Accord
Accord2y 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