• Advertisement

• ### Popular Now

• 12
• 14
• 13
• 10
• 11
• Advertisement
• Advertisement
• Advertisement

# Real time multi-key input handling in console?

This topic is 1151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I've sifted through a lot of topics on the internet (including looking at the code for this article, which looked useful but didn't explain much to me as a beginner) and I need a lightweight, simple method for handling real time input in the command prompt window. I was using the kbhit()/getch() combination for awhile, but I don't think I can make that work for say, if I'm holding a key down that alters the actions of pressing a different key, like if I hold the B button on a controller to run. I'm also using a game state driven structure and so I would think I'd need to make some kind of object to handle this stuff to make things cleaner.

The problem when I look at these is that I'm just not experienced in these methods or the lingo involved. They almost never explain things. I tend to learn pretty well through explicit definitions, rigorously varied examples, perhaps some underlying mechanics, and pro/con analyses. But most people just say "do this followed by this" and it discourages me because I like to micromanage when possible, and I can never get to a point where I know everything someone is saying unless I spend hours in Google. In this case I specifically do not want to put bulky libraries in my code because I have to keep the filesize ridiculously small, and I thought it might be easier if I coded my own method for my specific purposes so I don't have extraneous bits lying around. It'd be awesome if I could figure out controller input as well.

If someone could explain a good way to do this I'd be really grateful. Thanks!

Edited by Akashi

#### Share this post

##### Share on other sites
Advertisement
As far as I know you'll need to use something platform-specific. I don't know of any functions in stdio/conio that let you detect key state.

In Win32, here's one approach that works:

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
while (true)
{
bool shift = (GetAsyncKeyState(VK_SHIFT) & 0x8000) == 0x8000;
bool enter = (GetAsyncKeyState(VK_RETURN) & 0x8000) == 0x8000;

printf("%u %u\r", shift, enter);
}

return 0;
}

Edited by Nypyren

#### Share this post

##### Share on other sites

I need a lightweight, simple method for handling real time input in the command prompt window... I don't think I can make that work for say, if I'm holding a key down that alters the actions of pressing a different key, like if I hold the B button on a controller to run

What you are attempting is not really possible in the context of a command line.

You can take Nypren's route, and attempt to jack global input away from the terminal, but it is going to require separate implementations on each platform, and it's not immediately clear whether you will be able to implement this cleanly on all platforms.

#### Share this post

##### Share on other sites

Do you think _kbhit/getch() would be my best bet then? I'm probably not aiming to be crazy cross-platform. As long as it works for Windows I'm fine.

I thought of using DirectInput or something similar, but I'm not sure how it'll effect the filesize of the executable or if something like that would work with the console. Again, as long as it works with pretty much all versions of Windows I could maybe compromise with my limitations. I'm pretty much trying to make it so this could fit on a floppy disk.

Edited by Akashi

#### Share this post

##### Share on other sites

You'll likely get better responses if you describe more clearly what you want to do in general terms, rather than how you want to do it with specific functions.

You mention both  "command prompt window," and "executable." Working directly in a command prompt window, and running an application which does I/O using a console window are not the same.

Are you, in fact, asking about coding a console application?

#### Share this post

##### Share on other sites

I am trying to make a game in the console. (MS-DOS, right?) Here's an example from a really early build of the game. I'm trying to work under certain limitations.

So there's just some stuff I'm confused about and I'm not sure how to remedy. Like if I want to make it so you can "walk" unless you hold a button allowing you to "run". I could live without that feature, but if there's a way to do it I'd like to try and implement it. As long as I can make it compatible with most modern Windows OSs then I should be fine. I just want it done.

Edited by Akashi

#### Share this post

##### Share on other sites

I am trying to make a game in the console. (MS-DOS, right?) Here's an example from a really early build of the game. I'm trying to work under certain limitations.

So there's just some stuff I'm confused about and I'm not sure how to remedy. Like if I want to make it so you can "walk" unless you hold a button allowing you to "run". I could live without that feature, but if there's a way to do it I'd like to try and implement it. As long as I can make it compatible with most modern Windows OSs then I should be fine. I just want it done.

Just use GetAsyncKeyState then.

(MS-DOS, right?)

Not really

#### Share this post

##### Share on other sites

Huh, looks good. Thanks Nypren! I think this will work with what I'm trying to do.

#### Share this post

##### Share on other sites

• Advertisement