Advertisement

Spacebar question

Started by March 09, 2003 12:35 AM
5 comments, last by Delsaber 21 years, 8 months ago
c++ Ok this question is, how do I know if the user has pressed the space bar? For example if I wanted to do something after the user pressed ''k'' I could do something like:

keypressed=getch();
if(keypressed==''k'')||(keypressed==''K'')
{
...
}
 
but how would I get a spacebar hit from the user? If you could, can you guys also tell me how to detect other key hits from the user that aren''t obvious. Some others I would like to know are ctrl, esc or the arrow keys. Thanks
One of the old tricks I remember is that when you press an "advanced" key, the first one will return 0, then you will need to getch() again...

I don''t remember which keys qualify... but you can do this:

if (0 == a = getch()) b = getch();a = a+b<<8; // now you will have a 16-bit number that you can use to identify the key. 


I don''t even remember the last time I used that... I can''t even remember which keys it can get and which it can''t.

BTW -- I thought that the spacebar used the 0x32 character '' '') 
Advertisement
quote: Original post by Nypyren
One of the old tricks I remember is that when you press an "advanced" key, the first one will return 0, then you will need to getch() again...

I don''t remember which keys qualify... but you can do this:

if (0 == a = getch()) b = getch();a = a+b<<8; // now you will have a 16-bit number that you can use to identify the key.  


I don''t even remember the last time I used that... I can''t even remember which keys it can get and which it can''t.

BTW -- I thought that the spacebar used the 0x32 character '' '')  


Sry Nypyren but im confused. That code doesnt compile on my turbo c++ borland compiler. Also when I tried to put 0x32 character in a cout statement it just displayed ''50''. Sorry if I''m an idiot, but please help

(char)0x32

.lick
What about good old

    if(keypressed==' '){   // bla}    

?

And space has the ASCII value 32, which means you can either use char c = 0x20 or c = 32.

[edited by - BitMaster on March 9, 2003 8:16:04 AM]
quote: Original post by Anonymous Poster

Sry Nypyren but im confused. That code doesnt compile on my turbo c++ borland compiler. Also when I tried to put 0x32 character in a cout statement it just displayed ''50''. Sorry if I''m an idiot, but please help



AP: 0x32 is the hexadecimal representation for the number 50 (16*3+2). The character code for a space is just 32 (decimal), which is 0x20 in hex.

It''s worth mentioning that ''getch()'' is a function C++ inherited from C. It returns an ''int'' type, not a ''char'' type, so your ''keypressed'' variable is presumably defined as an ''int'' type too.

The expected behaviour for cout when given an ''int'' to stream is to convert the int to a string, hence you get a number, not the actual character.

To check for a space, you can compare with '' '', like this:

if ( '' ''==keypressed )   {       ... space was pressed ...   } 


The "Esc" key can be tested for using character code 27. In ASCII, this code represents "Escape".

You can also avoid having to test for both upper and lower case letters by using the "tolower()" function or a simple bit-clear operation (I think it''s bit #5) before performing the checks. Make sure you''ve tested that the value _is_ a letter first though!

It''s worth reading up on the ASCII codes and its history if you want to get the most out of it.

Note that "Alt", "Ctrl" and the Windows keys are not defined in the ASCII standard. You''ll need to check Turbo C++''s documentation to find the function(s) to use to test for these.

Hope this helps,

--
Sean Timarco Baggaley
Sean Timarco Baggaley (Est. 1971.)Warning: May contain bollocks.
Advertisement
Thanks, that helps a lot

This topic is closed to new replies.

Advertisement