Is it possible to call a specific method every time a button is pressed, even when the console is not expecting user input?Global keyboard capture in C# applicationWhat is the correct way to create a single-instance WPF application?What's the use/meaning of the @ character in variable names in C#?Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onWhy does C# forbid generic attribute types?Reading settings from app.config or web.config in .netHow to call a method daily, at specific time, in C#?Is is possible to programmatically clear the console history?Why must we define both == and != in C#?Why not inherit from List<T>?What does $ mean before a string?
Extension of trace on von Neumann subalgebra
Why did Steve Rogers choose Sam in Endgame?
Video editor for YouTube
Manually select/unselect lines before forwarding to stdout
Why do legislative committees exist?
If I stood next to a piece of metal heated to a million degrees, but in a perfect vacuum, would I feel hot?
Creating a character, is Noble a class or a background?
Can both line and load go to same screw on a GFCI outlet?
I do not have power to all my breakers
Clarification on defining FFT bin sizes
Mathematica function equivalent to Matlab's residue function (partial fraction expansion)
What is the superlative of ipse?
How could an animal "smell" carbon monoxide?
How to change checkbox react correctly?
Should I be able to keep my company purchased standing desk when I leave my job?
Was all the fuel expended in each stage of a Saturn V launch?
Doing research in academia and not liking competition
What alternatives exist to at-will employment?
Is there a way to handmake alphabet pasta?
Why run a service as a system user?
Why do candidates not quit if they no longer have a realistic chance to win in the 2020 US presidents election
How to get bold version of blackboard bold fonts?
Why did Spider-Man take a detour to Dorset?
Remove cardinal direction letters
Is it possible to call a specific method every time a button is pressed, even when the console is not expecting user input?
Global keyboard capture in C# applicationWhat is the correct way to create a single-instance WPF application?What's the use/meaning of the @ character in variable names in C#?Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onWhy does C# forbid generic attribute types?Reading settings from app.config or web.config in .netHow to call a method daily, at specific time, in C#?Is is possible to programmatically clear the console history?Why must we define both == and != in C#?Why not inherit from List<T>?What does $ mean before a string?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm making a program for differentiating functions and want to call a specific method as soon as "esc" is pressed.
So far I've tried finding an answer through Google, but I can't seem to find anything that works. Does anyone know if this is possible in a C# console application?
c# console console-application
add a comment |
I'm making a program for differentiating functions and want to call a specific method as soon as "esc" is pressed.
So far I've tried finding an answer through Google, but I can't seem to find anything that works. Does anyone know if this is possible in a C# console application?
c# console console-application
1
stackoverflow.com/questions/604410/…
– Dmitry Bychenko
Mar 26 at 7:34
add a comment |
I'm making a program for differentiating functions and want to call a specific method as soon as "esc" is pressed.
So far I've tried finding an answer through Google, but I can't seem to find anything that works. Does anyone know if this is possible in a C# console application?
c# console console-application
I'm making a program for differentiating functions and want to call a specific method as soon as "esc" is pressed.
So far I've tried finding an answer through Google, but I can't seem to find anything that works. Does anyone know if this is possible in a C# console application?
c# console console-application
c# console console-application
asked Mar 26 at 7:33
AndersAnders
93 bronze badges
93 bronze badges
1
stackoverflow.com/questions/604410/…
– Dmitry Bychenko
Mar 26 at 7:34
add a comment |
1
stackoverflow.com/questions/604410/…
– Dmitry Bychenko
Mar 26 at 7:34
1
1
stackoverflow.com/questions/604410/…
– Dmitry Bychenko
Mar 26 at 7:34
stackoverflow.com/questions/604410/…
– Dmitry Bychenko
Mar 26 at 7:34
add a comment |
1 Answer
1
active
oldest
votes
There is no event to listen for key presses. Only Console.ReadKey
. But you can turn it into a callback using Task
s. Like so:
public static void RegisterKeyHandler(Action<ConsoleKeyInfo> handler)
Task.Run(() =>
while (true)
var key = Console.ReadKey();
handler(key);
);
This only receives keys while the console window is focused.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351876%2fis-it-possible-to-call-a-specific-method-every-time-a-button-is-pressed-even-wh%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no event to listen for key presses. Only Console.ReadKey
. But you can turn it into a callback using Task
s. Like so:
public static void RegisterKeyHandler(Action<ConsoleKeyInfo> handler)
Task.Run(() =>
while (true)
var key = Console.ReadKey();
handler(key);
);
This only receives keys while the console window is focused.
add a comment |
There is no event to listen for key presses. Only Console.ReadKey
. But you can turn it into a callback using Task
s. Like so:
public static void RegisterKeyHandler(Action<ConsoleKeyInfo> handler)
Task.Run(() =>
while (true)
var key = Console.ReadKey();
handler(key);
);
This only receives keys while the console window is focused.
add a comment |
There is no event to listen for key presses. Only Console.ReadKey
. But you can turn it into a callback using Task
s. Like so:
public static void RegisterKeyHandler(Action<ConsoleKeyInfo> handler)
Task.Run(() =>
while (true)
var key = Console.ReadKey();
handler(key);
);
This only receives keys while the console window is focused.
There is no event to listen for key presses. Only Console.ReadKey
. But you can turn it into a callback using Task
s. Like so:
public static void RegisterKeyHandler(Action<ConsoleKeyInfo> handler)
Task.Run(() =>
while (true)
var key = Console.ReadKey();
handler(key);
);
This only receives keys while the console window is focused.
answered Mar 26 at 7:43
Rain336Rain336
6503 silver badges12 bronze badges
6503 silver badges12 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351876%2fis-it-possible-to-call-a-specific-method-every-time-a-button-is-pressed-even-wh%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
stackoverflow.com/questions/604410/…
– Dmitry Bychenko
Mar 26 at 7:34