Reading Keystates

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Reading Keystates

 
0
  #1
Aug 30th, 2004
I can read which key is pressed using getch(). For example i can read whether left arrow is pressed or not. But i want know whether both left arrow and up arrow is pressed at the same time or not-- I want to move an object diagonally across the screen if both LEFT+ UP arrow is pressed and that's why i want to know this thing.

Also how do i know whether a key is pressed or released.

Also while making games i noticed that I dont get smooth movement of the space-craft that i control. Like if I pushed right arrow(and kept it pressed) then it moves one step to right first time but then stops for a while and only then it keeps goin in the right direction as long as I keep the right arrow pressed. The problem here is the lag between the first time it moves and when it keeps goin in a direction. I want to eliminate the lag and thus give a smoother control.

I know all these problems can be solved using DirectInput, but i would like to know if i can do so in CONSOLE,

Thanx
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 217
Reputation: marceta is an unknown quantity at this point 
Solved Threads: 0
marceta marceta is offline Offline
Posting Whiz in Training

Re: Reading Keystates

 
0
  #2
Aug 30th, 2004
http://www.mkssoftware.com/docs/man3/curs_getch.3.asp

hope this helps you. let me know how it goes.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Reading Keystates

 
0
  #3
Aug 31st, 2004
well, i have seen the page but i could not find anything useful, there were something about reading function keys but nothing about reading reading multiple keys or different keystates.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 45
Reputation: BlackDice is an unknown quantity at this point 
Solved Threads: 0
BlackDice's Avatar
BlackDice BlackDice is offline Offline
Light Poster

Re: Reading Keystates

 
0
  #4
Aug 31st, 2004
Originally Posted by Asif_NSU
well, i have seen the page but i could not find anything useful, there were something about reading function keys but nothing about reading reading multiple keys or different keystates.
try GetAsyncKeyState() to get the key you want. As for the arrow key problem, you shouldn't rely on windows to handle your movements. Again, use GetAsyncKeyState(). If you're just using a basic windows message loop, within your main() add a call to a function called UpdateGame() or GameLoop(), or whatever you want to call it. When your application first starts, set up a global variable to hold the milliseconds elapsed for every loop something like this:

void GameLoop()
{
UINT nSeconds = timeGetTime()
//for your object to move every tenth of a second
if((gSeconds + 100) > nSeconds)
{
gSeconds = nSeconds;
if(GetAsyncKeyState(VK_UP))
gTop += 1;
if(GetAsyncKeyState(VK_DOWN))
gTop -= 1;
if(GetAsyncKeyState(VK_LEFT))
gLeft -= 1;
if(GetAsyncKeyState(VK_RIGHT))
gLeft += 1;
}
//do some type of update to your object's drawn position
m_Object.UpdatePosition(gLeft,gTop);
}

Having a function like this is how most games are done, but in the application's message loop, it's only done if the msg is WM_IDLE so that your game only uses idle time to update the game. you won't see any performance hits in your game, and it will allow for other events to transpire. Hope this helps. (this isn't the most efficient algorithm in the world, or the best, but I just put it together :lol: )
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 4
Reputation: GDFans is an unknown quantity at this point 
Solved Threads: 0
GDFans GDFans is offline Offline
Newbie Poster

Re: Reading Keystates

 
0
  #5
Sep 3rd, 2004
You can do this by capturing keybord interrupt.(09h?yes)
I remember I have seen an article solving this problem.Actually,it is not
an easy stuff,but Im not sure I can check it out.:~)

I think I have the same problem with you.Im thinking about reading data
from I/O directly,but I havnt done it yet.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 95
Reputation: gusano79 is on a distinguished road 
Solved Threads: 9
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Reading Keystates

 
1
  #6
Sep 8th, 2004
Originally Posted by GDFans
You can do this by capturing keybord interrupt.(09h?yes)
I remember I have seen an article solving this problem.Actually,it is not
an easy stuff,but Im not sure I can check it out.:~)
...
Correct; hardware interrupt 9h is the keyboard interrupt, and it's possible to use that to get keyboard states. But if you're compiling for Win32, then by all means use the API like bdiamond suggests--it's tougher to write your own int 9 handler, and what you get is something that works just like GetAsyncKeyState anyway.

Originally Posted by GDFans
...
I think I have the same problem with you.Im thinking about reading data
from I/O directly,but I havnt done it yet.
Dealing with the keyboard controller directly like that is a couple of orders of magnitude more insane than writing your own handler, trust me.

If you really want to play with hardware, the old, 16-bit DOS edition of the The Art of Assembly Language Programming has a chapter on the IBM PC keyboard and int 9 handling which I think is quite useful, if you don't mind x86 assembly.

--sg
Reply With Quote Quick reply to this message  
Reply

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




Views: 3536 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC