diff --git a/event/goEvent.h b/event/goEvent.h index cee1d85..b0ffd74 100644 --- a/event/goEvent.h +++ b/event/goEvent.h @@ -19,7 +19,247 @@ #include "pub.h" + void dispatch_proc(iohook_event * const event) { + char buffer[256] = { 0 }; + size_t length = snprintf(buffer, sizeof(buffer), + "id=%i,when=%" PRIu64 ",mask=0x%X", + event->type, event->time, event->mask); + + switch (event->type) { + case EVENT_KEY_PRESSED: + // If the escape key is pressed, naturally terminate the program. + if (event->data.keyboard.keycode == VC_ESCAPE) { + // int status = hook_stop(); + // switch (status) { + // // System level errors. + // case IOHOOK_ERROR_OUT_OF_MEMORY: + // loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status); + // break; + + // case IOHOOK_ERROR_X_RECORD_GET_CONTEXT: + // // NOTE This is the only platform specific error that occurs on hook_stop(). + // loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status); + // break; + + // // Default error. + // case IOHOOK_FAILURE: + // default: + // loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); + // break; + // } + } + case EVENT_KEY_RELEASED: + snprintf(buffer + length, sizeof(buffer) - length, + ",keycode=%u,rawcode=0x%X", + event->data.keyboard.keycode, event->data.keyboard.rawcode); + int akeyCode = (uint16_t) event->data.keyboard.keycode; + + if (event->data.keyboard.keycode == VC_ESCAPE + && atoi(cevent) == 11) { + int stopEvent = stop_event(); + // printf("stop_event%d\n", stopEvent); + cstatus = 0; + } + + // printf("atoi(str)---%d\n", atoi(cevent)); + if (akeyCode == atoi(cevent)) { + int stopEvent = stop_event(); + // printf("%d\n", stopEvent); + cstatus = 0; + } + break; + + case EVENT_KEY_TYPED: + snprintf(buffer + length, sizeof(buffer) - length, + ",keychar=%lc,rawcode=%u", + (uint16_t) event->data.keyboard.keychar, + event->data.keyboard.rawcode); + + #ifdef WE_REALLY_WANT_A_POINTER + char *buf = malloc (6); + #else + char buf[6]; + #endif + + sprintf(buf, "%lc", (uint16_t) event->data.keyboard.keychar); + + #ifdef WE_REALLY_WANT_A_POINTER + free (buf); + #endif + + if (strcmp(buf, cevent) == 0) { + int stopEvent = stop_event(); + // printf("%d\n", stopEvent); + cstatus = 0; + } + // return (char*) event->data.keyboard.keychar; + break; + + case EVENT_MOUSE_PRESSED: + case EVENT_MOUSE_RELEASED: + case EVENT_MOUSE_CLICKED: + case EVENT_MOUSE_MOVED: + case EVENT_MOUSE_DRAGGED: + snprintf(buffer + length, sizeof(buffer) - length, + ",x=%i,y=%i,button=%i,clicks=%i", + event->data.mouse.x, event->data.mouse.y, + event->data.mouse.button, event->data.mouse.clicks); + + int abutton = event->data.mouse.button; + int aclicks = event->data.mouse.clicks; + int amouse = -1; + + if (strcmp(cevent, "mleft") == 0) { + amouse = 1; + } + if (strcmp(cevent, "mright") == 0) { + amouse = 2; + } + if (strcmp(cevent, "wheelDown") == 0) { + amouse = 4; + } + if (strcmp(cevent, "wheelUp") == 0) { + amouse = 5; + } + if (strcmp(cevent, "wheelLeft") == 0) { + amouse = 6; + } + if (strcmp(cevent, "wheelRight") == 0) { + amouse = 7; + } + if (abutton == amouse && aclicks == 1) { + int stopEvent = stop_event(); + cstatus = 0; + } + + break; + + case EVENT_MOUSE_WHEEL: + snprintf(buffer + length, sizeof(buffer) - length, + ",type=%i,amount=%i,rotation=%i", + event->data.wheel.type, event->data.wheel.amount, + event->data.wheel.rotation); + break; + + default: + break; + } + + // fprintf(stdout, "----%s\n", buffer); +} + +int add_event(char *key_event) { + // (uint16_t *) + cevent = key_event; + // Set the logger callback for library output. + hookSetlogger(&loggerProc); + + // Set the event callback for IOhook events. + hook_set_dispatch_proc(&dispatch_proc); + // Start the hook and block. + // NOTE If EVENT_HOOK_ENABLED was delivered, the status will always succeed. + int status = hook_run(); + + switch (status) { + case IOHOOK_SUCCESS: + // Everything is ok. + break; + + // System level errors. + case IOHOOK_ERROR_OUT_OF_MEMORY: + loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status); + break; + + + // X11 specific errors. + case IOHOOK_ERROR_X_OPEN_DISPLAY: + loggerProc(LOG_LEVEL_ERROR, "Failed to open X11 display. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_NOT_FOUND: + loggerProc(LOG_LEVEL_ERROR, "Unable to locate XRecord extension. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_ALLOC_RANGE: + loggerProc(LOG_LEVEL_ERROR, "Unable to allocate XRecord range. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_CREATE_CONTEXT: + loggerProc(LOG_LEVEL_ERROR, "Unable to allocate XRecord context. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_ENABLE_CONTEXT: + loggerProc(LOG_LEVEL_ERROR, "Failed to enable XRecord context. (%#X)", status); + break; + + + // Windows specific errors. + case IOHOOK_ERROR_SET_WINDOWS_HOOK_EX: + loggerProc(LOG_LEVEL_ERROR, "Failed to register low level windows hook. (%#X)", status); + break; + + + // Darwin specific errors. + case IOHOOK_ERROR_AXAPI_DISABLED: + loggerProc(LOG_LEVEL_ERROR, "Failed to enable access for assistive devices. (%#X)", status); + break; + + case IOHOOK_ERROR_CREATE_EVENT_PORT: + loggerProc(LOG_LEVEL_ERROR, "Failed to create apple event port. (%#X)", status); + break; + + case IOHOOK_ERROR_CREATE_RUN_LOOP_SOURCE: + loggerProc(LOG_LEVEL_ERROR, "Failed to create apple run loop source. (%#X)", status); + break; + + case IOHOOK_ERROR_GET_RUNLOOP: + loggerProc(LOG_LEVEL_ERROR, "Failed to acquire apple run loop. (%#X)", status); + break; + + case IOHOOK_ERROR_CREATE_OBSERVER: + loggerProc(LOG_LEVEL_ERROR, "Failed to create apple run loop observer. (%#X)", status); + break; + + // Default error. + case IOHOOK_FAILURE: + default: + loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); + break; + } + + // return status; + // printf("%d\n", status); + return cstatus; +} + +int stop_event(){ + int status = hook_stop(); + switch (status) { + // System level errors. + case IOHOOK_ERROR_OUT_OF_MEMORY: + loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_GET_CONTEXT: + // NOTE This is the only platform specific error that occurs on hook_stop(). + loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status); + break; + + // Default error. + case IOHOOK_FAILURE: + default: + // loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); + break; + } + + return status; +} + +/* +qy custom to extent event listener +*/ +void dispatch_proc_listener(iohook_event * const event) { int keycode; @@ -62,7 +302,7 @@ int add_event_listener() { printf("start C hook_set_dispatch_proc\n"); // Set the event callback for IOhook events. - hook_set_dispatch_proc(&dispatch_proc); + hook_set_dispatch_proc(&dispatch_proc_listener); printf("start C hook_run\n"); // Start the hook and block. @@ -139,27 +379,4 @@ int add_event_listener() { } return cstatus; -} - -int stop_event(){ - int status = hook_stop(); - switch (status) { - // System level errors. - case IOHOOK_ERROR_OUT_OF_MEMORY: - loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status); - break; - - case IOHOOK_ERROR_X_RECORD_GET_CONTEXT: - // NOTE This is the only platform specific error that occurs on hook_stop(). - loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status); - break; - - // Default error. - case IOHOOK_FAILURE: - default: - // loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); - break; - } - - return status; } \ No newline at end of file diff --git a/event/goEvent_qy.h b/event/goEvent_qy.h new file mode 100644 index 0000000..cee1d85 --- /dev/null +++ b/event/goEvent_qy.h @@ -0,0 +1,165 @@ +// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// https://github.com/go-vgo/robotgo/blob/master/LICENSE +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// +// The hook directory link from the https://github.com/robotn/gohook/ +// hook, you need to follow the relevant agreement and LICENSE. +// See the LICENSE file at the top-level directory of this distribution and at +// https://github.com/robotn/gohook/blob/master/LICENSE + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include "pub.h" + +void dispatch_proc(iohook_event * const event) { + + int keycode; + + switch (event->type) { + case EVENT_KEY_PRESSED: + // If the escape key is pressed, naturally terminate the program. + if (event->data.keyboard.keycode == VC_ESCAPE) { + + } + case EVENT_KEY_RELEASED: + + keycode = (int) event->data.keyboard.keycode; + //printf("EVENT_KEY_RELEASED:%d\n", keycode); + showKeyCode(keycode); + //callback(keycode); + + break; + + case EVENT_KEY_TYPED: + + keycode = (int) event->data.keyboard.keycode; + //printf("EVENT_KEY_TYPED:%d\n", keycode); + + break; + + default: + break; + } +} + +int add_event_listener() { + + printf("start C add_event\n"); + + + // Set the logger callback for library output. + printf("start C hookSetlogger\n"); + + hookSetlogger(&loggerProc); + + printf("start C hook_set_dispatch_proc\n"); + // Set the event callback for IOhook events. + hook_set_dispatch_proc(&dispatch_proc); + + printf("start C hook_run\n"); + // Start the hook and block. + // NOTE If EVENT_HOOK_ENABLED was delivered, the status will always succeed. + int status = hook_run(); + + printf("hook_run status:%d\n", status); + + switch (status) { + case IOHOOK_SUCCESS: + // Everything is ok. + break; + + // System level errors. + case IOHOOK_ERROR_OUT_OF_MEMORY: + loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status); + break; + + + // X11 specific errors. + case IOHOOK_ERROR_X_OPEN_DISPLAY: + loggerProc(LOG_LEVEL_ERROR, "Failed to open X11 display. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_NOT_FOUND: + loggerProc(LOG_LEVEL_ERROR, "Unable to locate XRecord extension. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_ALLOC_RANGE: + loggerProc(LOG_LEVEL_ERROR, "Unable to allocate XRecord range. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_CREATE_CONTEXT: + loggerProc(LOG_LEVEL_ERROR, "Unable to allocate XRecord context. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_ENABLE_CONTEXT: + loggerProc(LOG_LEVEL_ERROR, "Failed to enable XRecord context. (%#X)", status); + break; + + + // Windows specific errors. + case IOHOOK_ERROR_SET_WINDOWS_HOOK_EX: + loggerProc(LOG_LEVEL_ERROR, "Failed to register low level windows hook. (%#X)", status); + break; + + + // Darwin specific errors. + case IOHOOK_ERROR_AXAPI_DISABLED: + loggerProc(LOG_LEVEL_ERROR, "Failed to enable access for assistive devices. (%#X)", status); + break; + + case IOHOOK_ERROR_CREATE_EVENT_PORT: + loggerProc(LOG_LEVEL_ERROR, "Failed to create apple event port. (%#X)", status); + break; + + case IOHOOK_ERROR_CREATE_RUN_LOOP_SOURCE: + loggerProc(LOG_LEVEL_ERROR, "Failed to create apple run loop source. (%#X)", status); + break; + + case IOHOOK_ERROR_GET_RUNLOOP: + loggerProc(LOG_LEVEL_ERROR, "Failed to acquire apple run loop. (%#X)", status); + break; + + case IOHOOK_ERROR_CREATE_OBSERVER: + loggerProc(LOG_LEVEL_ERROR, "Failed to create apple run loop observer. (%#X)", status); + break; + + // Default error. + case IOHOOK_FAILURE: + default: + loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); + break; + } + + return cstatus; +} + +int stop_event(){ + int status = hook_stop(); + switch (status) { + // System level errors. + case IOHOOK_ERROR_OUT_OF_MEMORY: + loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status); + break; + + case IOHOOK_ERROR_X_RECORD_GET_CONTEXT: + // NOTE This is the only platform specific error that occurs on hook_stop(). + loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status); + break; + + // Default error. + case IOHOOK_FAILURE: + default: + // loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status); + break; + } + + return status; +} \ No newline at end of file diff --git a/event/pub.h b/event/pub.h index 68d9af5..3d0e068 100644 --- a/event/pub.h +++ b/event/pub.h @@ -49,11 +49,14 @@ int rrevent; // uint16_t *cevent; int cstatus = 1; -typedef int (*fnc2)(int); // 回调函数的名称为 fnc2,参数是 char *str -extern int showKeyCode(int s ); +/*qy add to extend event listener*/ +extern int showKeyCode(int keyCode ); +int add_event_listener(); +//qy add end int stop_event(); -int add_event_listener(); +int add_event(char *key_event); + // int allEvent(char *key_event); int allEvent(char *key_event, int vcode[], int size); diff --git a/eventTest/output/eventTest b/eventTest/output/eventTest new file mode 100755 index 0000000..bb7da68 --- /dev/null +++ b/eventTest/output/eventTest @@ -0,0 +1 @@ +ELF \ No newline at end of file diff --git a/robotgo.go b/robotgo.go index c799575..ce87c43 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1260,6 +1260,13 @@ func AddEvent(key string) int { return geve } +func AddEventListener() int { + + C.add_event_listener() + + return 1 +} + // StopEvent stop event listener func StopEvent() { C.stop_event() diff --git a/writeTest.txt b/writeTest.txt deleted file mode 100644 index 985e56c..0000000 --- a/writeTest.txt +++ /dev/null @@ -1 +0,0 @@ -vvvvvbvvvv \ No newline at end of file