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
- Error in “MLSD” command While Connecting FTP to Server [closed]
- Floating point exception( core dump
- Java default constructor
- How do I make a delay in Java?
- Passing Arrays to Function in C++
- Return array in a function
- did you specify the right host or port? error on Kubernetes
- How does Java’s PriorityQueue differ from a min-heap?
- How does the modulus operator work?
- Single class has a Class Redefinition Error
- Identifier not found error on function call
- Debug assertion failed. C++ vector subscript out of range
- Transport endpoint is not connected
- What causes a SIGABRT fault?
- Graphics Library for C++
- ‘cout’ was not declared in this scope
- What is the difference between const int*, const int * const, and int const *?
- How to convert .crt to .pem [duplicate]
- Socket Programming in C++
- How do I split a string on a delimiter in Bash?
- Calculating Standard Deviation & Variance in C++
- warning: ISO C++ forbids variable length array
- cin >> “no operator matches these operands”
- C++ Structure Initialization
- Why does Wi-Fi have so much more bandwidth than Bluetooth?
- expression preceding parentheses of apparent call must have (pointer-to-) function type
- C++ #include guards
- glm rotate usage in Opengl
- Get dictionary value by key
- error: member access into incomplete type : forward declaration of
- Debug assertion failed
- “Implicit instantiation of undefined template” when forward declaring template class
- “…redeclared as different kind of symbol”?
- SwiftUI – How do I change the background color of a View?
- How to resolve the error java.net.SocketException: Too many open files
- Is there a standard C++ grammar?
- invalid use of non-static data member
- C: using strtol endptr is never NULL, cannot check if value is integer only?
- use of class template requires template argument list
- Why would this give a Use of uninitialised value of size 8
- C++ Error: Type Name is Not Allowed
- The system cannot find the file specified. in Visual Studio
- Python for C++ Developers
- Devise lockable – How to unlock account using unlock_in
- How to bind multiple values to a single WPF TextBlock?
- Remove last character from C++ string
- plotting package for c++
- ‘setprecision’ is not a member of ‘std’
- How to use stringstream to separate comma separated strings [duplicate]
- When/why to make function private in class?
- Using cin to input a single letter into a char
- C++ calling base class constructors
- Why am I getting an ‘Else without previous if’ error within a for loop?
- How to disable Windows Update Medic Service?
- How to solve “Unresolved inclusion:
” in a C++ file in Eclipse CDT? - Android Debugging Failing With “Couldn’t connect to logcat, GetProcessId returned: 0” FFImageLoading.Platform.dll.so Not Found
- How to store data in the flash memory of a Arduino Uno (ATmega328p)
- Is it still safe to delete nullptr in c++0x?
- What is a blob URL and why it is used?
- Declaration is incompatible with type
- One or more multiply defined symbols found
- I’m getting the error “stoi is not a member of std” in myprogramminglab [duplicate]
- Template constructor in a class template – how to explicitly specify template argument for the 2nd parameter?
- C++ float array initialization
- Maintenance holding page while migrating website
- How to get a current post’s blog id
- Will has_category be true is used on index and one of queried posts has the category?
- Exclude categories with only sticky posts OR count sticky post of specific category
- Any benefit or detriment from removing a pagefile on an 8 GB RAM machine?
- How do I view the details of a digital certificate .cer file?
- How do you validate fstab without rebooting?
- DNS – NSLOOKUP what is the meaning of the non-authoritative answer?
- Why can’t a CNAME record be used at the apex (aka root) of a domain?
- How can I upgrade to Java 1.8 on an Amazon Linux Server?
- What port should I open to allow remote desktop?
- Can you help me with my capacity planning?
- Cheat Sheets for System Administrators?
- Amazon EC2 terminology – AMI vs. EBS vs. Snapshot vs. Volume
- How to change an EC2 instance’s security group
- What type of DNS record is needed to make a subdomain?
- Multiple SSL domains on the same IP address and same port?
- IPTABLES – Limit rate of a specific incoming IP
- How to set default Ansible username/password for SSH connection?