Is there anyway i can detect "~" by using GetAsyncKeyState? - c++

By using GetAsyncKeyState it cannot detect '~' button (at the top left of the keyboard).
So there is any way to detect this button?
Or should I use another command ?
By the way I am using c++

The virtual key code of "~" is VK_OEM_3.
More virtual key codes can be referenced:
https://learn.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes
How to use it, please refer to MSDN
A simple example:
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
BOOL OEM_3 = FALSE;
while (1)
{
if (GetAsyncKeyState(VK_OEM_3) < 0 && OEM_3 == false)
{
//Press down
OEM_3 = true;
cout << "Press down" << endl;
}
if (GetAsyncKeyState(VK_OEM_3) >= 0 && OEM_3 == true)
{
//Release
OEM_3 = false;
cout << "Release" << endl;
}
}
return 0;
}

Related

How to trigger a function when control c is pressed in c++

I'm wondering how could I trigger a function when control + c is pressed but I want it not only in the program window but outside the window like in a browser, text pad and etc. Help would be appreciated.
This would be in C++
Thanks
You can do something like this on Windows:
char input = _getch();
if (input == '\x3')
{
std::cout << "Ctrl C pressed!" << std::endl;
//...
}
The above code will print "Ctrl C pressed!" when input == '\x3' (Control C).
Full example:
#include <iostream>
#include <conio.h>
void foo(char character)
{
std::cout << character << std::endl;
}
int main()
{
while (true)
{
char input = _getch();
if (input == '\x3')
{
foo(input);
}
}
}

Get current keystate?

I am trying to get current key state for key ALT?
But its not work,why?
Here is the code
#include <iostream>
#include <Windows.h>
bool KeyPressed(short p_key)
{
if (GetAsyncKeyState(p_key) & 0x8000)
{
std::cout << "KEYPRESSED";
return true;
}
else return false;
}
int main()
{
std::cout << "Test";
while (true)
{
KeyPressed(0x4A);
}
std::cout << "";
}
It should get the state everytime it check,but its not at all
Try to use something like this:
if (GetKeyState(VK_MENU) & 0x8000))
{
// ALT key is down.
}
VK_MENU is the virtual-key code for the ALT key
Found this information in following article:
https://learn.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input

How to wait for a specific keystroke?

I'm a bit new to C++, so I beg your pardon for being a bit nooby.
Is there a function I can use to make the console pause until a specific key is pressed?
Example being:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
if (specific key pressed) {
i = 1;
} else if (other key pressed) {
i = 2;
}
cout << i << endl;
return 0;
}
The console should output 1 if the right key is pressed, and 2 if another key is.
What you're trying to do is a bit more complex, C++ makes use of the cin stream where the input into the console is fed into your program. Where as a key-press event would be something the operating system would handle and would vary between operating systems. So using something like this would require the user to press enter/return for the input to be received by the program.
char key;
std::cin >> key;
if (key == 'a') {
std::cout << 1;
}
else {
std::cout << 2;
}
Find some answers here How to handle key press events in c++
Works on Windows only:
#include <iostream>
#include <vector>
#include <Windows.h>
char GetKey(std::vector<char> KeysToCheckFor)
{
while (true)
{
Sleep(1);
for (int i = 0; i < KeysToCheckFor.size(); i++)
{
if (GetKeyState(toupper(KeysToCheckFor[i])) < 0) { return KeysToCheckFor[i]; }
}
}
}
int main()
{
std::cout << "Press one of the keys: a,b,c\n";
char returnedkey = GetKey({ 'a', 'b', 'c' });
std::cout << returnedkey << " has been pressed!\n";
system("pause");
}

SDL inputs only working sometimes

I am experimenting with keyboard input in SDL and I have encountered a strange problem. Whenever I get input it only outputs the appropriate response sometimes (Clicking X only sometimes closes the program, pressing 1 only sometimes outputs "you pressed 1". Here is my main code:
#include <iostream>
#include <SDL.h>
#include "Screen.h"
#include "Input.h"
using namespace std;
int main(int argc, char *argv[]) {
Screen screen;
Input input;
if (screen.init() == false) {
cout << "Failure initializing SDL" << endl;
}
while (true) {
if (input.check_event() == "1") {
cout << "You pressed 1" << endl;
} else if (input.check_event() == "quit") {
break;
}
}
SDL_Quit();
return 0;
and here is my Input class:
#include <iostream>
#include <SDL.h>
#include "Input.h"
using namespace std;
string Input::check_event() {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return "quit";
}
else if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_1:
return "1";
}
}
}
return "null";
}
Any help would be appreciated!
From the documentation of SDL_PollEvent():
If event is not NULL, the next event is removed from the queue and
stored in the SDL_Event structure pointed to by event.
Analyzing your code:
if (input.check_event() == "1") {
This removes the event, whatever it is, from the queue.
} else if (input.check_event() == "quit") {
Say the return value of the 1st call to check_event() was "quit", then this call won't return "quit" again, because this information is now lost.
To fix that, call check_event() only once per loop iteration and store the result in a temporary variable. Then use only that variable in the conditions:
while (true) {
string event = input.check_event();
if (event == "1") {
cout << "You pressed 1" << endl;
} else if (event == "quit") {
break;
}
}

Why does this code execute two branches of if...else in VS Debug mode

I've met some problem in debug some code in vs2013/2015/2008.
#include <iostream>
using namespace std;
int main()
{
int mode = 1;
int size = 1;
if (mode == 0)
{
cout << "mode = 0" << endl;
}
else
{
if (mode == 1)
{
if (size > 0)
{
cout << "mode=1,size=1" << endl; //in debug mode will execute this sentence
}
else
return 0; //however,when I Step in (F10),this will also be executed but have no influence.But When I insert a breakpoint in there and press F5, will not break in there
}
else if (mode == 2)
{
if (size > 0)
{
cout << "mode=2,size=1" << endl;
}
else
return 0;
}
}
return 1;}
So, how to explain this phenomenon? Does it have any problem? And how can I solve it?
This is how the debugger works in VS 2015. Ignore it.
return 0; isn't executed, even if it looks like it. Weird behavior, I was surprised by that too.
They should have made the arrow go to the else statement, not the first line after it, but for some reason they didn't.
Just ignore it. You can make a feature request if you want too :)