How to get the last key pressed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluesteel
    New Member
    • Mar 2007
    • 84

    How to get the last key pressed?

    Hi guys, this is what i wrote:


    if(kbhit()){
    a=[some function returning last key pressed];
    doaction(a);
    }

    doaction() is a function i wrote but it doesnt matter. What does matter is how to get a character from keyboard without interrupting execution just like getch() does. I am thinking of making a game so i need the game not to wait for the player to press a key, the game should go on. By using kbhit() i know whether a key was pressed, but i dunno which key. That is the problem i am currently facing. If there is something i didn't write in a clear way, please let me know it.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I would do this by registering a signal handler with whatever system your using. I don't know what its called in Windows (yes, I assume your using windows) but there is always some way you can say "whenever event X occurs, call function Y" - just request to have your function called whenever a key is pressed, that way instead of polling all the time looking for presses your app gets notified of when one comes in.

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      Oh, I just looked at what kbhit() does... It looks like you might be able to use getch() like this

      Code:
      if( kbhit())
      {
          a = getch();
      }

      Comment

      • bluesteel
        New Member
        • Mar 2007
        • 84

        #4
        Originally posted by mac11
        I would do this by registering a signal handler with whatever system your using. I don't know what its called in Windows (yes, I assume your using windows) but there is always some way you can say "whenever event X occurs, call function Y" - just request to have your function called whenever a key is pressed, that way instead of polling all the time looking for presses your app gets notified of when one comes in.
        But i mean, how can i do that in c++, the one way i can think of is by "while"
        but i still can't get the last key pressed

        Comment

        • bluesteel
          New Member
          • Mar 2007
          • 84

          #5
          Originally posted by bluesteel
          But i mean, how can i do that in c++, the one way i can think of is by "while"
          but i still can't get the last key pressed
          Also, by using getch(), program is paused and that is what i want to avoid.
          I use kbhit() to know whether a key is pressed but i need to know what key.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by bluesteel
            Also, by using getch(), program is paused and that is what i want to avoid.
            I use kbhit() to know whether a key is pressed but i need to know what key.
            If kbhit() is true, then getch() should return right away with a value.


            Adrian

            Comment

            • bluesteel
              New Member
              • Mar 2007
              • 84

              #7
              Originally posted by AdrianH
              If kbhit() is true, then getch() should return right away with a value.


              Adrian
              It does work, but as long as you don't write too many instructions under the same while (the one checking if kbhit()). Anyway, thank you very much, i have solved a great problem i was facing. Again thank you very much and i love this website, because every question gets its answer!!!!!!!

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                Originally posted by bluesteel
                It does work, but as long as you don't write too many instructions under the same while (the one checking if kbhit()). Anyway, thank you very much, i have solved a great problem i was facing. Again thank you very much and i love this website, because every question gets its answer!!!!!!!
                Glad you are happy with TSDN, we try our best.


                Adrian

                Comment

                • Parikshitdagdu
                  New Member
                  • Dec 2007
                  • 1

                  #9
                  Originally posted by bluesteel
                  Hi guys, this is what i wrote:


                  if(kbhit()){
                  a=[some function returning last key pressed];
                  doaction(a);
                  }

                  doaction() is a function i wrote but it doesnt matter. What does matter is how to get a character from keyboard without interrupting execution just like getch() does. I am thinking of making a game so i need the game not to wait for the player to press a key, the game should go on. By using kbhit() i know whether a key was pressed, but i dunno which key. That is the problem i am currently facing. If there is something i didn't write in a clear way, please let me know it.

                  HINT:

                  I am also in the search of answer of the same problem. However what i
                  know till now is that by using signal handling mechanism provided by operating system, we come to the answer.
                  Signals in the system can be catogorised as synchronous signal and
                  asynchronous signals. whenever program encounters a faulty instruction such as divide by zero, trap occurs in the system. System then, in turn, generates
                  signal to the program informing of the faulty instructions and then terminates the program. This signal is the one that is generated by faulty instruction in the program itself hence called as synchronous signal. On the other hand when
                  signal is generated as a result of an event ( such as pressing key ) external to the program . It is called as asynchronous signal.
                  There are another issues concerning how program should handle
                  such signal. Amongst them are simply ignore signal, perform action according to system defined function such as terminating the program, Or handle it according to code defined by application programmer like you and me.
                  I suggest you further to read book Operating System Principles by
                  Galvin for motivation purpose (Second chapter specifically, where this mechanism is illustrated in a program). Then try for signal handling mechanism from the books you may pursue later.
                  Nowadays I am busy with my schedule and not able to spend time
                  for reading so I think you may proceed further and reply me for the answer.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    All you should have to do is process a WM_KEYDOWN in your window procedure and case out the virtual key code.

                    http://msdn2.microsoft.com/en-us/library/ms645540.aspx.

                    Comment

                    • Studlyami
                      Recognized Expert Contributor
                      • Sep 2007
                      • 464

                      #11
                      Originally posted by bluesteel
                      It does work, but as long as you don't write too many instructions under the same while (the one checking if kbhit()).
                      You could run that while loop in a new thread. This will allow the while loop to run while your other game code is running (the while loop won't lag up the main loop). Look up some information on multi-threading.

                      Comment

                      • codeit02
                        New Member
                        • May 2014
                        • 1

                        #12
                        Originally posted by mac11
                        Oh, I just looked at what kbhit() does... It looks like you might be able to use getch() like this

                        Code:
                        if( kbhit())
                        {
                            a = getch();
                        }

                        whenever a key is pressed then kbhit() returns a non-zero value and the IF condition satisfies but my doubt is as we have already pressed the key why we are using getch() inside
                        IF statement.

                        Comment

                        Working...