how to get the last key pressed without stopping the programm ?

Reply

Join Date: Jan 2005
Posts: 2
Reputation: WARburg is an unknown quantity at this point 
Solved Threads: 0
WARburg WARburg is offline Offline
Newbie Poster

how to get the last key pressed without stopping the programm ?

 
0
  #1
Jan 22nd, 2005
Hello Everyone!
That's my first post on this forum and I'd like to greet Everyone

I got a question about getting the last key pressed without stopping the programm. I know that getchar(), cin, getch() commands don't allow to do that. Have You any idea how to solve this problem?

I 'd like to do sth like that:

while(play){
char a;
a=get_last_key_without_stopping_the_programm(); // Do You know a function similiar to that?
switch(a){
case: 'W' :
do_sth();
break;
case 'S' :
do_sth_else();
break;
default:
do_default();
break;
}
a='*';
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to get the last key pressed without stopping the programm ?

 
0
  #2
Jan 22nd, 2005
>I know that getchar(), cin, getch() commands don't allow to do that.
getch (and getche) does, but it isn't a standard function and won't exist on all implementations. What compiler and operating system do you use?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 10
Reputation: Emmitt310 is an unknown quantity at this point 
Solved Threads: 0
Emmitt310 Emmitt310 is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #3
Jan 22nd, 2005
Well, i think i know what you mean but it may help to know what kind of program you want to make. If it's a win32 application, checking for keyboard input during a loop is easy, but if this isnt a win32 application, keyboard input can still be checked, its just a bit more complicated.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 3
Reputation: mango_maniac is an unknown quantity at this point 
Solved Threads: 0
mango_maniac mango_maniac is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #4
Jan 22nd, 2005
Originally Posted by Emmitt310
Well, i think i know what you mean but it may help to know what kind of program you want to make. If it's a win32 application, checking for keyboard input during a loop is easy, but if this isnt a win32 application, keyboard input can still be checked, its just a bit more complicated.
First post here...I found this site looking for similar information. I'm using win32, but am totally unfamilliar with the windows.h header. I currently only know standard c++. How would one go about capturing keystrokes without waiting for <return> to be pressed. If you could demonstrate a compilable snippet, I'd be grateful.

MM
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 10
Reputation: Emmitt310 is an unknown quantity at this point 
Solved Threads: 0
Emmitt310 Emmitt310 is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #5
Jan 22nd, 2005
Ok well, the reason why it's easy in win32 to check for keyboard input is because you can check for a window message WM_KEYDOWN. Each key has a specific virtual key code, but, you can figure that out. Heres a simple code snippet that checks for if the key pressed was the esc key. (this is compilable if you know where to put it in your program)

  1. switch(message)
  2. {
  3. case WM_KEYDOWN:
  4. switch(wParam)
  5. {
  6. case VK_ESCAPE: //the virutal key code for esc
  7. PostQuitMessage(0);
  8. break;
  9. }
  10. break;
  11. /*
  12.   rest of the window procedure
  13.   */
  14. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 3
Reputation: mango_maniac is an unknown quantity at this point 
Solved Threads: 0
mango_maniac mango_maniac is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #6
Jan 23rd, 2005
Originally Posted by Emmitt310
Ok well, the reason why it's easy in win32 to check for keyboard input is because you can check for a window message WM_KEYDOWN. Each key has a specific virtual key code, but, you can figure that out. Heres a simple code snippet that checks for if the key pressed was the esc key. (this is compilable if you know where to put it in your program)

  1. switch(message)
  2. {
  3. case WM_KEYDOWN:
  4. switch(wParam)
  5. {
  6. case VK_ESCAPE: //the virutal key code for esc
  7. PostQuitMessage(0);
  8. break;
  9. }
  10. break;
  11. /*
  12.   rest of the window procedure
  13.   */
  14. }
Thanks for your help. I ended up doing it another way. I'm going to use something like the following. Do you see problems other than non-portability to systems that don't use ascii 13 for <return>?
  1. #include <windows.h>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. string getPassword();
  8.  
  9. int main(){
  10.  
  11. string password("true");
  12.  
  13. while (true){
  14.  
  15. cout << " Input Password: ";
  16. password = getPassword();
  17.  
  18. cout << "Verify Password: ";
  19. string pwverify = getPassword();
  20.  
  21. if(password == pwverify) {break;}
  22. cout << "\nPassword mismatch. Try again\n";
  23. }
  24.  
  25. cout << password << endl << password.size() << endl;
  26.  
  27. system("pause");
  28. }
  29.  
  30.  
  31. string getPassword(){
  32. CHAR buffer[1] = "";
  33. DWORD w;
  34. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  35. HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
  36. INPUT_RECORD input[1];
  37.  
  38. string pw;
  39.  
  40. while(true) {
  41. // Read an input record.
  42. ReadConsoleInput(keyboard, input, 1, &w);
  43.  
  44. // Process a key down input event.
  45. if(input[0].EventType == KEY_EVENT
  46. && input[0].Event.KeyEvent.bKeyDown)
  47. {
  48.  
  49. // Retrieve the character that was pressed.
  50. buffer[0] = input[0].Event.KeyEvent.uChar.AsciiChar;
  51.  
  52. if(buffer[0] == 13){ break;} // enter pressed
  53. if(buffer[0] == 8){ // backspace
  54. pw.erase(pw.size()-1, 1);
  55. cout << "\b \b";
  56. continue;
  57. }
  58.  
  59. if(buffer[0] != 0) { // 0 is function keys, shift, ctrl, etc.
  60. cout << '*';
  61. pw += buffer[0];
  62. }
  63. }
  64. }
  65. cout << endl;
  66. return pw;
  67. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 2
Reputation: WARburg is an unknown quantity at this point 
Solved Threads: 0
WARburg WARburg is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #7
Jan 23rd, 2005
Originally Posted by Narue
>I know that getchar(), cin, getch() commands don't allow to do that.
getch (and getche) does, but it isn't a standard function and won't exist on all implementations. What compiler and operating system do you use?
I use Linux and Windows OS both and I'd like to make an application that is compatible with both of them(or needs just a few little changes). I try to make some snake-similar game in ASII. Compiler for Windows I use is Borland c++ builder 6.0 and for Linux g++).
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: Quetzalcoatl is an unknown quantity at this point 
Solved Threads: 0
Quetzalcoatl Quetzalcoatl is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #8
Jun 22nd, 2005
How would I do a similar thing using TC(required for school), which cannot use Windows.h? (Uses DOS)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 1
Reputation: Marcosp is an unknown quantity at this point 
Solved Threads: 0
Marcosp Marcosp is offline Offline
Newbie Poster

Re: how to get the last key pressed without stopping the programm ?

 
0
  #9
Apr 7th, 2006
About the solution given by Mango Maniac, does anyone knows how to do the same but using a Matlab mex file? I runned a modified version of his program in Visual C++ and it worked, but when I put the same code in the Matlab "mexfuction" the program compile and runs, but it doesn't respond when I press the keys. Here is the code:

#include <windows.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include "C:\MATLAB7\extern\include\mex.h"


CHAR buffer[1] = "";
DWORD w;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD input[1];

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) {



mexPrintf("para cima");
while (1) {
ReadConsoleInput(keyboard, input, 1, &w);
if(input[0].EventType == KEY_EVENT && input[0].Event.KeyEvent.bKeyDown) {
buffer[0] = input[0].Event.KeyEvent.uChar.AsciiChar;
if(buffer[0] == 13) { break;}
if(buffer[0] == 56) {mexPrintf("para cima");}
if(buffer[0] == 50) {mexPrintf("para baixo");}
if(buffer[0] == 52) {mexPrintf("esquerda");}
if(buffer[0] == 54) {mexPrintf("direita");}

}
}
}

I'm using a windows XP, Matlab 7 and Visual C++ 6.0
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC