Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. testing for <esc> key

testing for <esc> key

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testingtutorialquestion
6 Posts 5 Posters 35 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RalfPeter
    wrote on last edited by
    #1

    Hi all, I need to abort a procedure upon the user hitting the key. Could anyone point me in the right direction on how to implement it? Thx, Ralf. ralf.riedel@usm.edu

    S C 2 Replies Last reply
    0
    • R RalfPeter

      Hi all, I need to abort a procedure upon the user hitting the key. Could anyone point me in the right direction on how to implement it? Thx, Ralf. ralf.riedel@usm.edu

      S Offline
      S Offline
      Selevercin
      wrote on last edited by
      #2

      Well, if it's a console app (not sure for others... may or may not work) you can do something like the following: if (kbhit()) { if (getch() == 26) { // escape key was pressed! } } And I believe you have to include conio.h. Also, I'm not positive 26 is the escape key... but my gut says it is. And if you are costantly looping through that code, you will know when the escape key was pressed. Good luck. If you have a problem with my spelling, just remember that's not my fault. I (as well as everyone else who learned to spell after 1976) blame it on Robert A. Kolpek for U.S. Patent 4,136,395.

      C 1 Reply Last reply
      0
      • R RalfPeter

        Hi all, I need to abort a procedure upon the user hitting the key. Could anyone point me in the right direction on how to implement it? Thx, Ralf. ralf.riedel@usm.edu

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        You need to either run your procedure in a seperate thread or use GetAsyncKeyState. To check for escape, I reckon the constant is VK_ESC, but that's a guess. This function tells you if a key was pressed since it was last called, so call it, then do something like: if (GetAsyncKeyState(VK_ESC)) { // finish off and go home } Actually, the most significant bit is set if the key is down now, and the least significant MAY be set if it has been pressed, so MSDN tells me. The above is how I always did it, and I'd say it's more efficient than bit mashing over and over inside your process. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

        J A 2 Replies Last reply
        0
        • S Selevercin

          Well, if it's a console app (not sure for others... may or may not work) you can do something like the following: if (kbhit()) { if (getch() == 26) { // escape key was pressed! } } And I believe you have to include conio.h. Also, I'm not positive 26 is the escape key... but my gut says it is. And if you are costantly looping through that code, you will know when the escape key was pressed. Good luck. If you have a problem with my spelling, just remember that's not my fault. I (as well as everyone else who learned to spell after 1976) blame it on Robert A. Kolpek for U.S. Patent 4,136,395.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Escape is 27, from memory. However, it's bad to rely on these values, you should use the virtual key constants instead. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          1 Reply Last reply
          0
          • C Christian Graus

            You need to either run your procedure in a seperate thread or use GetAsyncKeyState. To check for escape, I reckon the constant is VK_ESC, but that's a guess. This function tells you if a key was pressed since it was last called, so call it, then do something like: if (GetAsyncKeyState(VK_ESC)) { // finish off and go home } Actually, the most significant bit is set if the key is down now, and the least significant MAY be set if it has been pressed, so MSDN tells me. The above is how I always did it, and I'd say it's more efficient than bit mashing over and over inside your process. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

            J Offline
            J Offline
            jmkhael
            wrote on last edited by
            #5

            VK_ESCAPE Papa while (TRUE) Papa.WillLove ( Bebe ) ;

            1 Reply Last reply
            0
            • C Christian Graus

              You need to either run your procedure in a seperate thread or use GetAsyncKeyState. To check for escape, I reckon the constant is VK_ESC, but that's a guess. This function tells you if a key was pressed since it was last called, so call it, then do something like: if (GetAsyncKeyState(VK_ESC)) { // finish off and go home } Actually, the most significant bit is set if the key is down now, and the least significant MAY be set if it has been pressed, so MSDN tells me. The above is how I always did it, and I'd say it's more efficient than bit mashing over and over inside your process. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Thanks for the input. Your suggestion worked just fine. Best, Ralf.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups