Skip to content

Instantly share code, notes, and snippets.

@NBonaparte
Created September 7, 2018 22:20
Show Gist options
  • Save NBonaparte/900611f343c5692c83068397e742c8e2 to your computer and use it in GitHub Desktop.
Save NBonaparte/900611f343c5692c83068397e742c8e2 to your computer and use it in GitHub Desktop.
Simple program to translate mouse movements to arrow keys with libxdo
#include <xdo.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
static bool x_axis, y_axis;
static const char *up = "Up";
static const char *down = "Down";
static const char *left = "Left";
static const char *right = "Right";
static useconds_t delay = 20;
static int deadzone = 10;
static struct timespec slp_tm = {
.tv_sec = 0,
.tv_nsec = 8e6,
};
int main() {
xdo_t *xdo = xdo_new(NULL);
if (xdo == NULL) {
printf("Failed to create xdo_t instance\n");
exit(1);
}
Window win, win_prev;
unsigned char *name;
int x, y, x2 = -1, y2 = -1;
int name_len, name_type;
while(1) {
xdo_get_mouse_location2(xdo, &x, &y, NULL, &win);
xdo_get_window_name(xdo, win, &name, &name_len, &name_type);
printf("%s\n", (char *) name);
if (abs(x2 - x) <= deadzone) {
xdo_send_keysequence_window_up(xdo, win_prev, x_axis ? right : left, delay);
printf("Releasing %s\n", x_axis ? right : left);
} else if (x2 > -1) {
x2 = x;
nanosleep(&slp_tm, NULL);
continue;
}
if (abs(y2 - y) <= deadzone) {
xdo_send_keysequence_window_up(xdo, win_prev, y_axis ? down : up, delay);
printf("Releasing %s\n", y_axis ? down : up);
} else if (y2 > -1) {
y2 = y;
nanosleep(&slp_tm, NULL);
continue;
}
xdo_wait_for_mouse_move_from(xdo, x, y);
xdo_get_mouse_location(xdo, &x2, &y2, NULL);
if (x2-x > deadzone) {
x_axis = true;
xdo_send_keysequence_window_down(xdo, win, right, delay);
printf("Pressing right\n");
} else if (x - x2 > deadzone) {
x_axis = false;
xdo_send_keysequence_window_down(xdo, win, left, delay);
printf("Pressing left\n");
}
if (y2-y > deadzone) {
y_axis = true;
xdo_send_keysequence_window_down(xdo, win, down, delay);
printf("Pressing down\n");
} else if (y - y2 > deadzone) {
y_axis = false;
xdo_send_keysequence_window_down(xdo, win, up, delay);
printf("Pressing up\n");
}
//x = x2;
//y = y2;
win_prev = win;
nanosleep(&slp_tm, NULL);
}
xdo_free(xdo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment