473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
May 3 '07 #1
11 8962
mac11
256 100+
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.
May 3 '07 #2
mac11
256 100+
Oh, I just looked at what kbhit() does... It looks like you might be able to use getch() like this

Expand|Select|Wrap|Line Numbers
  1. if( kbhit())
  2. {
  3.     a = getch();
  4. }
May 3 '07 #3
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
May 8 '07 #4
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.
May 8 '07 #5
AdrianH
1,251 Expert 1GB
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
May 8 '07 #6
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!!!!!!!
May 9 '07 #7
AdrianH
1,251 Expert 1GB
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
May 10 '07 #8
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.
Dec 19 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 19 '07 #10
Studlyami
464 Expert 256MB
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.
Dec 19 '07 #11
@mac11

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.
May 17 '14 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: josh dismukes | last post by:
/// here is the code i'm getting a parse error on the last line of the code which /// is </html> any help will be much appreciated. <?php session_start ();
3
by: James McGivney | last post by:
I have a project in VS.NET using C# I have a series of buttons on an aspx page. When one of the buttons is pressed, a panel becomes visible and allows the user to enter and edit data. I want to...
3
by: BLUE WATER | last post by:
Hi, I have managed to simplify my problem but can't seem to get this to work. I want to open up a form from a click event of another form, but not only open the form when the button is pressed...
2
by: Peder Y | last post by:
Anyone knows if there is some kind of property or function that will return the last selected/deselected item/index in a multiselect ListBox? SelectedIndex will point to first index in the...
5
by: Bill | last post by:
Hello, Could anyone post some simple code or advise me on how I can display the SSN number like *****7890 in a text box, even thought the user entered 1234567890, and the value of the variable...
19
by: darrel | last post by:
On my vb.net page, I have 4 sets of inputs + form buttons. example: Search: (GO) Zip: (GO) County: (GO) County: (GO) The problem is if I go to the page, type in a zip code, and hit...
1
by: Hamed | last post by:
Hello How can I identify what was the last pressed key in a DataGrid or in a Form? Thanks in advance Hamed
3
by: mmckinnon | last post by:
Hi All, This is really basic but I'm having trouble finding a straight forward way to do this. I have an Access application where the for is in spreadsheet mode. When a user leaves a control...
1
by: AdamOnAccess | last post by:
I'm in Access 2007. I built a feature that to saves the current list in a sub form to a separate table. It works like this: After entering a list of words in the subform, the user can choose to push...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.