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

Recommended Answers

All 5 Replies

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.

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: )

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.

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.

...
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

commented: useful info, I appreciate--- Asif_NSU +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.