help with _getch()

i am using _getch() to detect keyboard input and i am working on my project on a mobile IDE (Cxxdroid) while i am travelling etc, and on my computer when i am at home.
here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <conio.h>

int main()
{
	while (1)
	{
		if (_kbhit())         // <--  is this part redundant???
		{
  			int keyboardInput;
			keyboardInput = _getch();
			std::cout << keyboardInput;
		}
	}
	return 0;
}


on the computer this works great, i press a key and the numeric value is printed instantly. but on the mobile the first key press does not print anything, then the second key press prints the first value, and the third key prints the second value and so on, always printing the key pressed two keystrokes ago.
what is going wrong on the mobile? and how can i make this work properly on both?

also, as a side question, is the _kbhit() part of the code redundant? i dont see a reason why it is needed and it appears to work fine without it.

thanks
Last edited on
getch is nonstandard so it may not behave as expected on a different compiler or system.

it sounds like you have a buffer problem making you off by one. you could try clearing cin's buffer?

kbhit is also nonstandard.

getch is blocking. kbhit is not, its an event. so if you didn't have the kbhit, it would block until a letter was typed, and that being in a tight loop, would block the program pretty much forever. kbhit avoids the blocking so the user types nothing, nothing is done.
cout is buffered. you can also try a flush in the if statement to see if that fixes your odd behavior.
thanks for the reply.
i tried flushing by using endl, instead of \n, which did not work either. i will try flushing the cin buffer next time i can play with it.

if getch and kbhit are non standard would they still work okay on different platforms once the program is built? (i think the word im looking for is built? i mean, once it is a program ready for somebody else to use on another system, like a .exe). or would they potentially cause problems down the line?

thanks

Once a program is built (compiled and linked into a final executable), that is when it is the most platform-dependent. An ".exe" file is mainly a Windows thing; if you have an .exe file it will probably work on another Windows machine, but even then there can be subtle incompatibilities for different versions of Windows.

When your original post says "on the computer", I assume that means Windows. <conio.h> is a Windows header. And a really old one at that. I don't know Cxxdroid, but if it's an app on mobile devices, that probably means it's compiling a program that is compatible with Android. I'm surprised that your code example even compiles on a mobile platform.

Does Cxxdroid provide any comprehensive documentation? I can't find any.

Overall, I don't think doing programming on mobile platforms is a very popular thing. To clarify, I mean the actually process of writing code. Of course, programs that are made for mobile devices are more and more popular every day, but most of those apps are still built on desktops, targeting a particular mobile platform. e.g. Android Studio.
Last edited on
I don't know current state but you used to be able to get a 'vm' or emulator for the target so you could test it without leaving the PC.
Yeah that's a common practice. I think there's also ways to debug a connected device over an active connection from the IDE, but I haven't done that.
thanks for the replies <3

yes i am on windows 7 64bit.
Cxxdroid doesnt have any documentation built in and i couldnt find anything online either.

i guess it is just down to the platform conflict or something then. its not a huge problem as i only really use the mobile version for learning (although it does mean i will have to carefully test anything i learn on a real computer).

@jonnin
the compiler i have is not for mobile apps. although i assume it can be used for that, but thats not what i am using it for.
i am just using it to learn C++ for the sake of building PC programs. i use the mobile version to learn while i am away from home (maybe i should save for a laptop! XD ) although i do hope to learn to write for mac, linux, mobile etc in the future.

If it's just for learning, you could just avoid using things like _kbhit(), _getch() and other platform-specific things in <conio.h>.
I suggest learning the core of the C++ language (syntax, variables, loops, functions, classes, etc.) and its standard library instead. The stuff in <conio.h> is just extra stuff that isn't necessary.
Last edited on
i have been learning the core stuff and i have a pretty good grasp on the things you mentioned, apart from classes, i am still learning a lot about those (although i am sure there is much more to learn about the rest too).

sooner or later i am going to need to learn about keyboard input though. i do feel like its something i should be learning at this point. i also learn better while writing programs with a goal in mind. it scatters my learning but i am in no rush at this point.

i assume by what you are saying that there are better ways of detecting and handling keyboard input though? can you suggest a better way if there is one please?

thanks for the help <3
i assume by what you are saying that there are better ways of detecting and handling keyboard input though?

When it comes to C++, what I am most familiar with is standard input. C++ takes input in as streams. This includes standard input ("stdin", AKA std::cin), and files (std::fstream).
The most basic way to send input is to type it into the terminal or console window (command-line input). And the usual way to then trigger this information to be sent to the program is to hit Enter/Return. You can do the vast majority of, and very useful, things with just this.

For example, this is how you would ask for the user to enter an integer in C++:
1
2
3
4
int n;
cout << "Enter an integer: ";
cin >> n;
cout << "You entered: " << n << '\n';

Line-delimited input can be obtained through getline, see example at: http://www.cplusplus.com/reference/string/string/getline/

That is how you get user input in C++: through standard input. (Sorry if you consider this really basic and already knew that, I just wanted to make sure we were on the same page)

But this is not "real-time"; the program waits for input and the processes it. Once you start trying to get fancy things like real-time keyboard input, you're going beyond what basic input streams in C++ were meant for, because handling input in this way requires system-specific commands (like getch()) to interact with your terminal/console.

There are three main types of user interfaces: Command-Line Interface (CLI), Text-based User Interface (TUI), and Graphical User Interface (GUI). [I guess "voice-based interfaces" are getting popular these days, with all those "virtual assistant" products out there]

If you want to use TUIs, more effort is going to be needed to ensure it can work on multiple platforms. I don't often use TUIs, so I can't give much advice here. <conio.h> is a way to make some basic TUI-like stuff in Windows (originally for DOS), but it's not cross platform and is really old.
There is a well-known library called ncurses which "allows the programmer to write text-based user interfaces in a terminal-independent manner."
If you do a search for "ncurses android" or "tui android", you'll get results for what looks like how to make mobile-compatible TUIs. But again, I don't know much about that. To me, using a mobile device as a TUI sounds really painful.

If you want to eventually make GUI applications on Android (as in, fully fledged graphical interface with touchscreen, swiping, buttons, other widgets), developer.android.com has a lot of resources on this, and there's plenty of information to search for on the web. Most of the examples are Java-based, but I think there are ways to program in C++ as well.
Last edited on
c++'s lack of a standardized getch / getche pair is exceedingly frustrating. you have to use something third party here.
i should have been more clear in my intentions, sorry about that.
i am writing a game type input, where you would not see the characters appear on screen and you would not press enter. the buttons represent actions, so the keyboard input would then be filtered to execute events, such as a player moving.
the cout section of the code was just for testing purposes, so i could see things were working.

also, the mobile app compiler is just to use while i am away from the house, i am not actually trying to create mobile apps (yet).

i will look into the other input methods suggested though, hopefully something will be more effective and up to date.
is it typical to write different input code sections for different platforms if your program is cross platform?

thanks for the help
is it typical to write different input code sections for different platforms if your program is cross platform?

yes and no. Its typical in games for some high performance areas, including hardware interfaces. Usually its just in the code a few times with #defines around it so you can pick what code is built with compiler defines set. The amount of code that isn't portable is a lot less than it was long ago, but c++ still needs SOME of it if you port to drastically different platforms. Make it as clean and abstract as possible. Avoid it as much as possible.


okay that makes a lot of sense, thanks <3
Topic archived. No new replies allowed.