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;








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?










share|improve this question

















  • 1





    stackoverflow.com/questions/604410/…

    – Dmitry Bychenko
    Mar 26 at 7:34

















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?










share|improve this question

















  • 1





    stackoverflow.com/questions/604410/…

    – Dmitry Bychenko
    Mar 26 at 7:34













0












0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












1 Answer
1






active

oldest

votes


















0














There is no event to listen for key presses. Only Console.ReadKey. But you can turn it into a callback using Tasks. 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.






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    There is no event to listen for key presses. Only Console.ReadKey. But you can turn it into a callback using Tasks. 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.






    share|improve this answer



























      0














      There is no event to listen for key presses. Only Console.ReadKey. But you can turn it into a callback using Tasks. 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.






      share|improve this answer

























        0












        0








        0







        There is no event to listen for key presses. Only Console.ReadKey. But you can turn it into a callback using Tasks. 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.






        share|improve this answer













        There is no event to listen for key presses. Only Console.ReadKey. But you can turn it into a callback using Tasks. 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 7:43









        Rain336Rain336

        6503 silver badges12 bronze badges




        6503 silver badges12 bronze badges
















            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.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript