As mentioned by others there’s no cross platform way to do this, but on Windows you can do it like this:
The Code below checks if the key ‘A’ is down.
if(GetKeyState('A') & 0x8000/*Check if high-order bit is set (1 << 15)*/) { // Do stuff }
In case of shift or similar you will need to pass one of these: https://msdn.microsoft.com/de-de/library/windows/desktop/dd375731(v=vs.85).aspx
if(GetKeyState(VK_SHIFT) & 0x8000) { // Shift down }
The low-order bit indicates if key is toggled.
SHORT keyState = GetKeyState(VK_CAPITAL/*(caps lock)*/); bool isToggled = keyState & 1; bool isDown = keyState & 0x8000;
Oh and also don’t forget to
#include <Windows.h>
Related Posts:
- Why do we need virtual functions in C++?
- How do I fix a “Expected Primary-expression before ‘)’ token” error?
- What are the rules of the std::cin object in C++?
- C++ code file extension? What is the difference between .cc and .cpp
- C++ Error: ISO C++ Forbids Comparison Between Pointer and Integer [-fpermissive]
- segmentation fault 11 in C++ on Mac
- How to append text to a text file in C++?
- In C++, what is wx.h?
- Using “super” in C++
- new types may not be defined in a return type – C++
- declaring a priority_queue in c++ with a custom comparator
- C++ undefined reference to WinMain@16 (Code::Blocks)
- bit vectors in c++
- Very basic inheritance: error: expected class-name before ‘{’ token
- mkdir c++ function
- Error: “expected primary-expression before int”
- Operator Overloading C++; too many parameters for << operation
- Using prevNext Modx Addon
- Is “delete this” allowed in C++?
- :wq! command in vim
- how to set the background color of the status bar during the launching phase [duplicate]
- api-ms-win-crt-runtime-l1-1-0.dll is missing when opening Microsoft Office file [closed]
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- When is K 1024 and when is it 1000?
- What is a MIME type?
- How to create a dynamic array of integers
- Logitech/LGHUB Lua – Loop with break
- How to create a dynamic array of integers
- are there dictionaries in javascript like python?
- How can I convert MP3 file to a Base64 encoded string? [closed]
- Understanding The Modulus Operator %
- Understanding the main method of python [duplicate]
- Dial pad to get phone number (with Android button images)
- Linker Error C++ “undefined reference ” [duplicate]
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- How is the AND/OR operator represented as in Regular Expressions?
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Istio Ingress resulting in “no healthy upstream”
- Why is it not possible to fake an IP address?
- How to open a “-” dashed filename using terminal?
- How do I build a graphical user interface in C++? [closed]
- C++ convert from 1 char to string?
- How do I build a graphical user interface in C++? [closed]
- Substring in excel
- What is a LAMP stack?
- convert a char* to std::string
- What is a sanity test/check
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- What is the difference between POST and PUT in HTTP?
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- How to implement 2D vector array?
- What is the difference between POST and PUT in HTTP?
- Why the switch statement cannot be applied on strings?
- How to find Google’s IP address?
- What exactly is a VBO in OpenGL?
- What and where are the stack and heap?
- What is the difference between g++ and gcc?
- ping response “Request timed out.” vs “Destination Host unreachable”
- Authentication versus Authorization
- What is a reverse shell?
- How does npm start work? What all processes are happening in the background?
- What is an instance variable in Java?
- What is the := operator?
- What is tail recursion?
- Discord music bot not working
- What does “:=” mean in Pseudocode? [closed]
- How to use setprecision in C++
- Why am I getting error for apple-touch-icon-precomposed.png
- How to dynamically allocate arrays in C++
- What does (~0L) mean?
- How to dynamically allocate arrays in C++
- No results found on kibana -> discover
- What is Xpenology? Is it Linux related thing?
- What is the best way to use a HashMap in C++?
- What is the best way to use a HashMap in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- Why in C++ do we use DWORD rather than unsigned int? [duplicate]
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- Sleep for milliseconds
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- Easiest way to convert int to string in C++
- What is the difference between float and double?
- Why is “using namespace std;” considered bad practice?
- What is a CSRF token? What is its importance and how does it work?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- What is the C++ function to raise a number to a power?
- outputting ascii table in C++
- Create blank image in Imagemagick
- What is a lambda expression in C++11?
- Vector of Vectors to create matrix
- How to use the PI constant in C++
- Official definition of CSCI (Computer Software Configuration Item)
- How to throw a C++ exception
- std::string to char*
- How could I ping @here in Discord.py?
- How to generate a random number in C++?