❔ 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
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
when you press a chosen key in the console or just anywhere?
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#?
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)
first you register a hotkey, and then in the loop of your application you can use
PeekMessage
to see if the hotkey has been pressedthat shows C++ code
but i want to be able to just stop even while console isnt active
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
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...
maybe a bit overkill but a nice abstraction
How else will you establish a global keylogger?
Is it that easy? Enlighten me
well if its just 1 key to stop the application, a hotkey is easier to do
Wouldnt dare messing with P/Invoke and prefer a C# lib
"just for a hotkey"
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)
it can look intimidating, but its pretty simple, as you can see in the loop
inb4 youre going to bluescreen using that
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
Id rather have the code for a hotkey be shorter than my actual code
hide it in another file
?
namespaces, classes, functions is just hiding, organizing and reusing code, this hotkey stuff looks like a lot bcuz its not hidden away
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.