You are here:

C++/Working with keyboard


I am trying to create a game in which there will be falling items and I have to use the keyboard to control the paddle which has to collect the falling items.The only problem in this which concerns me is that my displayItems() and getKey() are two different functions.So, I wont be able to display the items continuously unless and until the user gives an input.And,I wish to display it continuously irrespective of the user entering or not entering the key

As you give no details of what platform, tools, frameworks, etc. you are using nor what the application type (GUI / console) is I cannot be give any sure, specific advise.

However if you use writing console based program (as per standard C++) then there is no way to do what you wish using only standard C++ library facilities.

Instead you need to check for the presence of keyboard input in a non-blocking manner.

If you are using Microsoft Visual C++ (or even as far back as Microsoft C for MS-DOS if I remember correctly) then the runtime library includes a function called _kbhit (note: early versions may only have a function kbhit - but this name is depreciated from Visual C++ 2005). See the MSDN library or the documentation for a specific Microsoft C/C++ compiler, e.g.:

(note: as I promise in my instructions to questioners I will point you to MSDN for Microsoft related development queries).

As the documentation states you have to include conio.h - note that this is _not_ a standard C or C++ header.
Some other compilers and their supporting libraries that target Microsoft Windows (or maybe even MS-DOS) may also support this function or have a similar alternative - if you are using such tools then I suggest you refer the documentation (online or manual etc.) for the tools in question.

If you are not using a Microsoft Windows based system then you will have to research what similar alternatives there are for the platform in question. For example on Linux or UN*X type systems you can probably use the curses or ncurses libraries' getch or related function in no-delay mode (note: you may need to install a developer curses or ncurses package):

Some other curses/ncurses references that might be of use: (plus other YouTube videos for other parts of the series)

If you are writing a GUI program then as they tend to use a reactive, message based approach to doing things it is probable that each key press would be presented as one or more messages or events to be processed within the main program processing loop which would be interleaved with the display drawing. This is certainly true of Microsoft Windows programs. The exact details differ between GUI systems - and would again have different conventions if you are using a GUI framework library such as wxWidgets (, Qt (, MFC (, etc.

You will notice that this answer refers you many Internet resources. I do not just keep all these URLs to hand, rather I use a search engine (Google in my case) to find what I require. I strongly suggest you do likewise in the future as there is an awful lot of technical resources out there on the Internet and you can usually find something that leads you to useful information.

Hope you find this of some use.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.