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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현