xevent.xkey.keycode not working for me (X11) - c

So I have this piece of code which works well
while(1) {
XNextEvent(dpy,&e);
paint(cs,g);
if(e.xbutton.button == 1) {
evolue(g,gc,cycleG);
paint(cs,g);
printf("test");
}else if(e.xbutton.button==3) break ;
}
here xbutton.button == 1 is the left click.
I want to change the code so that it does the function with a keyboard press by changing it to
while(1) {
XNextEvent(dpy,&e);
paint(cs,g);
if(e.xkey.keycode == 54) {
evolue(g,gc,cycleG);
paint(cs,g);
printf("test");
}else if(e.xbutton.button==3) break ;
}
I got the keycode 54 by running xev on the terminal
so anyways logically the second piece of code should work but it just doesn't run the code inside the if statement unlike when it took mouse input. So..what gives? And how to fix it taking keyboard input?
Edit:
for further testing I wrote this code
while(1) {
XNextEvent(dpy,&e);
if(e.type == KeyPress){
paint(cs,g);
evolue(g,gc,cycleG);
}
if(e.type == ButtonPress){
paint(cs,g);
evolue(g,gc,cycleG);
}
}
so that it executes the program no matter what mouse button is pressed and no matter what keyboard key is pressed, but again, only mouse buttons execute the program while keyboard keys dont.

The problem was in a function above that chooses the input type it was this
XSelectInput(dpy, win, ExposureMask|ButtonPressMask);
I changed it to this
XSelectInput(dpy, win, ExposureMask|ButtonPressMask|KeyPressMask);
and this fixed it.

Related

How can I make this while loop faster on a microcontroller button?

I am working with a micro controller that has a button A. When I press this button and hold it for 2 seconds, its value becomes 0 and the color turns either blue or green, when I release, its value goes back to 1 but the color stays the same unless it was clicked again and the color changes. The problem is, it shouldn't take someone 2 whole seconds to have to change the led light. What can I do to make the value (either 0 or 1) be read faster?
Here is a snippet of the code in the while loop.
// here are the states for reference. Everything is either a 0 or a 1
const int BUTTON_PRESSED = 0;
const int BUTTON_UNPRESSED = 1;
const int GREEN_LED = 0;
const int BLUE_LED = 1;
const struct timespec sleepTime = { 1, 0 };
while (true) {
Value_Type value;
// this function get the input of button a when pressed
GetValue(button_A_fd, &value);
Log_Debug(
"Button value (%d)\n", value);
// Processing the button.
//Turns LED ON; Button not pressed down
if (value == BUTTON_UNPRESSED) {
last_button_state = BUTTON_UNPRESSED;
} else {
// if last button state is 1 then now it is being pressed
if (last_button_state == BUTTON_UNPRESSED) {
// Flip LEDs
if (active_led == BLUE_LED) {
active_led = GREEN_LED;
}
else if (active_led == GREEN_LED) {
active_led = BLUE_LED;
}
last_button_state = BUTTON_PRESSED;
// sets the pointer to the 0 bit of the file to write
lseek(fd_storage, 0, SEEK_SET);
// write current active led to mutable storage and save
write(fd_storage, &active_led, sizeof(active_led));
}
}
// Blinking the active LED.
// reading input only when pressed and turn off other led
if (active_led == GREEN_LED) {
// turn off blue led, then turn on green
SetValue(blue_led_fd, Value_High);
SetValue(green_led_fd, Value_Low);
nanosleep(&sleepTime, NULL);
SetValue(green_led_fd, Value_High);
nanosleep(&sleepTime, NULL);
}
else if (active_led == BLUE_LED) {
// turn off green led, then turn on blue
SetValue(green_led_fd, Value_High);
SetValue(blue_led_fd, Value_Low);
nanosleep(&sleepTime, NULL);
SetValue(blue_led_fd, Value_High);
nanosleep(&sleepTime, NULL);
}
}
}
I tried to put GetValue() in several parts of the code to see if it could maybe get a value faster but it did not work. How can I move from here? I hope I shared enough code to understand the problem. Thank you.
Looks like your code reads the button which is fast, and then immediately sets outputs and sleeps 1 second, sets an output and sleeps another second before going up to check if the button is pressed again.
You should restructure the code to check the state of the button more frequently where you're going to sleep now.
Sleep for shorter periods and check to see if the button is pressed in a loop until your total sleep time has been met or the button state changes.
Upon further inspection, I found these:
The above picture is linked from here , it is the spec sheet for your microcontroller.
You want to look at the "Board Pin Map section" from this link to match it with the spec sheet of the chip itself

Constantly check for keyboard input while theres already a loop

Im creating a little game thats like a keyboard version of guitar hero. I'm almost done, but i'm just confused over one thing. I want to constantly be checking if the player is pressing anything on the keyboard and to see if they are pressing the correct key, but I already have a loop that sleeps every 1 second to update the game, so I cant have another loop running at the same time to constantly check the keyboard input (or can I?). I tried putting the keyboard check inside the game loop, but since it sleeps every second, it sometimes doesnt catch when the player presses the key.
This is my while loop:
while (playing)
{
updateBoard(score, game);
checkKeyPress(game);
Sleep(time);
if (rand() % 2 == 1)
spawnNewLetter(game);
}
updateBoard just prints out the new board, spawnNewLetter is just to spawn a new letter to fall down the board, and checkKeyPress is to check the keyboard input, this is it:
void checkKeyPress(char game[GAME_ROW][GAME_COL])
{
if (_kbhit())
{
switch (_getch())
{
case 97:
checkLetter(game, 'a');
break;
case 98:
checkLetter(game, 'b');
break;
The function goes on, but its just repeating to check for all the letters.
Any help would be appreciated, thanks!

Why does an empty printf allow me to continue reading data from the stdin?

CODE
while (1)
{
keycode = key_hook();
if (keycode == SPACE || keycode == BKSPACE)
{
render_again = 1;
}
if (keycode == ESC)
break;
if (render_again)
{
render_again = 0;
render(all);
}
dprintf(1, ""); //I have no idea why this prevents the program from freezing
}
int key_hook()
{
char buffer[4];
read(0, buffer, 4);
return (*(unsigned int *)buffer);
}
Alright, so this piece of code handles redrawing of text on screen. Some rows of text are underlined or highlighted using termcaps (tputs(tgetstr("us, NULL")......). Everything prints fine but after the first redraw of the text the while apparently freezes unless a dprintf/printf is present. The key_hook function just reads 4 bytes from the stdin and converts them to an int.
When I last did work here, my version of key_hook had a loop of single byte reads. This was broken by an alarm of 1 second and logic to whether the data so far was a key prefix.
The alarm interrupted the read, and stopped freeze

cvWaitKey() is not working?

I'm reading a book. The author explains the function clearly. The problem is that my program doesn't detect any key that is pressed, so I can't terminate the window or stop a loop. This is the part of the problem.
while(1)
{
frame = cvQueryFrame( capture );
if ( !frame )
break;
cvShowImage("Example2", frame);
char c = cvWaitKey(33);
if ( c == 27 )
{
printf("Yes");
break; // 27 == escape button
}
}
I'm using Mac (I'm running the program from the terminal).
There might be 2 possibilities ,
The focus is not on the cv window when you press the key.
Try using just, cvWaitKey().

How do i stop the blocking?

Hey all I've asked this question a few times in the past few days but I just don't get it...I basically want to have the while loop for the Beep command executed in the background while the user can interact with the available case statements (only one shown..there are others)....i keep getting blocked and everytime i want the beep to make a sound constantly i block the rest of my program...I have to use Beep so please don't suggest any other functionality..
here's a sample code...
while( keypress != 'q' || keypress != 'Q')
{
x = Beep(x);
while (x == 1)
Beep(350,300);
alarm_t current;
keypress = _getch();
switch(keypress){
case 'h':
sprintf_s(current.message,"high alarm");
current.timeOfEvent = time(NULL);
recordEvent(current);
break;
Now...my issue is with the while loop and the Beep command....here is what i call to Beep(x)
int Beep(int y)
{
return y;
}
So basically i am trying to call a function outside of my current cpp file to just compare x and y, and return y as being equivalent to x...i thought this might avoid blocking but it doesn't...
Your while loop around beep just won't work and _getch is blocking. So it will just block until there's a character.
Depending what platform you are on, you need something like kbhit (and if you google that you will find alternatives for other platforms). ie it's not standard C functionality and platform specific.
kbhit will return true or false depending if there is a character or not.
So you can do:
while(!key_is_quit(ch))
{
Beep();
if(kbhit())
{
ch = getch();
// switch....
}
}

Resources