GetAsyncKeyState(VK_ESCAPE) in c++ Linux

Apr 10, 2016 at 6:54am
GetAsyncKeyState(VK_ESCAPE) can be used with windows.h but in linux how this can be used.
please don't mention std::cin.get() or getch()
because it waits for user to input a ch but GetAsyncKeyState(VK_ESCAPE) works continually.
any alternative of GetAsyncKeyState(VK_ESCAPE) in LINUX ?
Last edited on Apr 10, 2016 at 6:56am
Apr 10, 2016 at 7:55am
I'm not sure there is something exactly like GetAsyncKeyState but you should be able to read key presses without delay using the ncurses library.
Apr 10, 2016 at 10:07am
thanks peter
but i google and found
This way the program is dynamically linked to the ncurses library. To run it in another computer, the system must have the ncurses library installed.

any other suggestion?
Last edited on Apr 10, 2016 at 10:08am
Apr 10, 2016 at 1:46pm
next line ...
If you want to avoid the trouble, you may have it statically linked.


See also
http://stackoverflow.com/questions/3514852/statically-link-ncurses-to-program
Last edited on Apr 10, 2016 at 1:47pm
Apr 11, 2016 at 1:07am
Is it for a windowed application or a console application?
Apr 11, 2016 at 5:21am
this will be used in linux console.
I successful written a program for windows now I want to convert my code for linux.
Apr 16, 2016 at 8:08am
Can your program run as root?

A method I've used to read keypresses was to directly read /dev/input/eventN.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <iostream>
...
void GetKeys(char* kbd_dev) 
{
  // kbd_dev should be keyboard device found in /dev/input (need root by default)
  int kbd = open(kbd_dev, O_RDONLY);
  if(kbd != -1)
  {
    input_event ie;
    while(read(kbd, &ie, sizeof(ie)))
    {
      std::cout << "Key Code: " << ie.code << "    Value: " << ie.value << "\n";
    }
    close(kbd);
  }
}
Last edited on Apr 16, 2016 at 8:09am
Apr 29, 2016 at 7:46am
my code is something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <windows.h> //works in win but doesnt in linux
#include <iostream>
void Esc();
void Start();

void Esc()
{
	//std::cout<<"press esc to exit! \n";
	if (GetAsyncKeyState(VK_ESCAPE)) // how to use such thing in linux?
	{
	return;
}
	else
	Start();
	
}

void Start()
{
	std::cout<< "hello again \n"; 
        // i want to keep seeing this msg "hello again" 
        //again and again without pressing any key just ESC to terminate
	Sleep (1000);
	Esc();
	return;
}
int main()
{
    std::cout<< "starting... press esc to exit!\n";
	Sleep (1000); //in linux we use sleep or usleep. in win s is capital
	Start();
    Esc();

    std::cout<<"exited: \n";
    Sleep (1000);

    return 0;
}
Last edited on Apr 29, 2016 at 7:49am
Topic archived. No new replies allowed.