•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,024 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,704 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 9860 | Replies: 8
![]() |
•
•
Join Date: Jan 2005
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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='*';
}
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='*';
}
>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?
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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Jan 2005
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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
•
•
Join Date: Sep 2004
Posts: 10
Reputation:
Rep Power: 5
Solved Threads: 0
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)
switch(message)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE: //the virutal key code for esc
PostQuitMessage(0);
break;
}
break;
/*
rest of the window procedure
*/
}•
•
Join Date: Jan 2005
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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)
switch(message) { case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: //the virutal key code for esc PostQuitMessage(0); break; } break; /* rest of the window procedure */ }
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>?
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
string getPassword();
int main(){
string password("true");
while (true){
cout << " Input Password: ";
password = getPassword();
cout << "Verify Password: ";
string pwverify = getPassword();
if(password == pwverify) {break;}
cout << "\nPassword mismatch. Try again\n";
}
cout << password << endl << password.size() << endl;
system("pause");
}
string getPassword(){
CHAR buffer[1] = "";
DWORD w;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD input[1];
string pw;
while(true) {
// Read an input record.
ReadConsoleInput(keyboard, input, 1, &w);
// Process a key down input event.
if(input[0].EventType == KEY_EVENT
&& input[0].Event.KeyEvent.bKeyDown)
{
// Retrieve the character that was pressed.
buffer[0] = input[0].Event.KeyEvent.uChar.AsciiChar;
if(buffer[0] == 13){ break;} // enter pressed
if(buffer[0] == 8){ // backspace
pw.erase(pw.size()-1, 1);
cout << "\b \b";
continue;
}
if(buffer[0] != 0) { // 0 is function keys, shift, ctrl, etc.
cout << '*';
pw += buffer[0];
}
}
}
cout << endl;
return pw;
}•
•
Join Date: Jan 2005
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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?
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
#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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Detecting any key being pressed. (VB.NET)
- checking string of key pressed (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Sorting structures
- Next Thread: random matrix generation



Linear Mode