Skip to content

Commit

Permalink
Adding a timeout after 650ms when holding CTRL, after which xcape wil…
Browse files Browse the repository at this point in the history
…l not generate Escape if released. Thanks to Richard for the suggestion.
  • Loading branch information
alols committed Apr 28, 2012
1 parent 3da651d commit 8144c42
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions xcape.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
Expand All @@ -43,6 +44,7 @@ typedef struct _XCape_t
pthread_t sigwait_thread;
sigset_t sigset;
Bool debug;
struct timeval down_at;
} XCape_t;


Expand Down Expand Up @@ -214,6 +216,7 @@ void intercept (XPointer user_data, XRecordInterceptData *data)
{
if (self->debug) fprintf (stdout, "Control pressed!\n");
ctrl_pressed = True;
gettimeofday (&self->down_at, NULL);

if (mouse_pressed)
{
Expand All @@ -225,14 +228,24 @@ void intercept (XPointer user_data, XRecordInterceptData *data)
if (self->debug) fprintf (stdout, "Control released!\n");
if (ctrl_used == False)
{
if (self->debug) fprintf (stdout,
"Generating ESC!\n");

XTestFakeKeyEvent (self->ctrl_conn,
self->escape_key, True, 0);
XTestFakeKeyEvent (self->ctrl_conn,
self->escape_key, False, 0);
XFlush (self->ctrl_conn);
struct timeval timev, ms650 = {
.tv_sec = 0,
.tv_usec = 650000
};
gettimeofday (&timev, NULL);
timersub (&timev, &self->down_at, &timev);

if (timercmp (&timev, &ms650, <))
{
if (self->debug) fprintf (stdout,
"Generating ESC!\n");

XTestFakeKeyEvent (self->ctrl_conn,
self->escape_key, True, 0);
XTestFakeKeyEvent (self->ctrl_conn,
self->escape_key, False, 0);
XFlush (self->ctrl_conn);
}
}
ctrl_pressed = False;
ctrl_used = False;
Expand Down

0 comments on commit 8144c42

Please sign in to comment.