Key Press Detection

I am new to programming. And i am learning through tutorials and making little programs myself.But i've faced a problem.I am using Code::Blocks to program in C++ but i cannot find a way to detect a key press, exept with getch() but i dont like getch sooo..Can somebody help me out.I've look through a ton of forum in about this topic but i was never able to find a clear anwser and something that actually works. Thanks! :)
Assuming you're running on Windows and you don't want to use conio functions then you should investigate either the Windows API, or perhaps the PDCurses library.
If you are only allergic to getch() and not conio in general you can use _kbhit(), Microsoft claims it is conformant to ISO C++

_kbhit(): https://msdn.microsoft.com/en-us/library/58w7c94c.aspx
Microsoft on _kbhit(): https://msdn.microsoft.com/en-us/library/ms235390.aspx
Microsoft claims it is conformant to ISO C++

The only reason it is "conformant" to ISO C++ is because it uses the leading underscore character to denote that the function is "implementation defined". This function is still not a standard C or C++ function.

That link: https://msdn.microsoft.com/en-us/library/ms235390.aspx is really only telling you that the POSIX version of the function is depreciated. Really it is telling you that the whole POSIX subsystem was depreciated by Microsoft since Windows XP.

Use the ISO C++ conformant _kbhit instead.
... also appear on the same website
Yes, they appear, but it is also a very misleading statement. These functions are not defined in either the C nor the C++ standard, therefore they are not standard functions, but the standard does allows implementations to define their own functions to "enhance" their product. So by renaming the function to "_kbhit()" or "_getch()" you now have functions that qualify as an implementation defined function.

From one of the C++14 draft standards:

17.6.4.3.2 Global names [global.names]
Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.
Each name that begins with an underscore is reserved to the implementation for use as name in the global namespace.


Last edited on
Can you show me an exaple how to use _kbhit() with a specific key?
but it is also a very misleading statement

well there's only one thing you can do then, get in touch with Microsoft and let them know rather than moaning about it here

OP: _kbhit() will work with at least any of the letter and number keys, see the example in the link I sent you
Last edited on
Here is an example how to use it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
  while (! _kbhit())
  {
    cout << '.';
  }

  return 0;
}


Any key key you press will exit the program.
well there's only one thing you can do then, get in touch with Microsoft and let them know rather than moaning about it here

Why should I "get in touch with Microsoft"? You're the one that doesn't understand the difference between a "conformant" function and a "standard" function.

But i want to make it for exaple when i press the letter 'g' it then activates
At the very outset I said
Microsoft claims
, did I not? Now which bit of this don't you understand
I understand none of it. I just want an example.
ha! ha! sorry OP, you're just caught in the cross-fire with our resident moaner
At the very outset I said
Microsoft claims
, did I not? Now which bit of this don't you understand

Ah, so then you admit that you're also being very misleading? If you knew before hand that it was a misleading statement you wouldn't have propagated the statement, would you?
More moans ... ;)
Are you guys going to show me an example or should i just close the threth cuz you are not being any help!
But i want to make it for exaple when i press the letter 'g' it then activates

You stated you didn't want to use getch()/_getch() so have you investigated the Windows API or PDCurses? Or did you decide that using getch() would be acceptable?

Perhaps something like (untried):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
  while (! _kbhit())
  {
       char value;
       if((value = _getch()) == 'g')
           cout << value;
  }

  return 0;
}


Thanks !
Topic archived. No new replies allowed.