Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keyboard event listener ,can run fine on win10,but when i run go get ,still has error, need help!thank you![don't merge] #163

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
534 changes: 534 additions & 0 deletions base/zconf.h

Large diffs are not rendered by default.

1,912 changes: 1,912 additions & 0 deletions base/zlib.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cdeps/hook/windows/hook_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ IOHOOK_API int hook_run() {
while (GetMessage(&message, (HWND) NULL, 0, 0) > 0) {
TranslateMessage(&message);
DispatchMessage(&message);

printf("&message: %s",&message);
}
}
else {
Expand Down
127 changes: 126 additions & 1 deletion event/goEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void dispatch_proc(iohook_event * const event) {
",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
Expand Down Expand Up @@ -254,4 +254,129 @@ int stop_event(){
}

return status;
}

/*
qy custom to extent event listener
*/
void dispatch_proc_listener(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_listener);

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;
}
165 changes: 165 additions & 0 deletions event/goEvent_qy.h
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 <config.h>
#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;
}
5 changes: 5 additions & 0 deletions event/pub.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ int rrevent;
// uint16_t *cevent;
int cstatus = 1;

/*qy add to extend event listener*/
extern int showKeyCode(int keyCode );
int add_event_listener();
//qy add end

int stop_event();
int add_event(char *key_event);

// int allEvent(char *key_event);
int allEvent(char *key_event, int vcode[], int size);

Expand Down
32 changes: 32 additions & 0 deletions eventTest/eventTest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

package main
/*
#cgo LDFLAGS: -L ../event/
#include <stdio.h>
#include <stdlib.h>
#include "../event/goEvent.h"
*/
import "C"

func event() {
////////////////////////////////////////////////////////////////////////////////
// Global event listener
////////////////////////////////////////////////////////////////////////////////

C.add_event_listener()
// stop AddEvent
// StopEvent()
}

func main() {
event()
}
23 changes: 23 additions & 0 deletions eventTest/exportFunc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

package main

import "C"
import (
"fmt"
)

//export showKeyCode
func showKeyCode(keyCode C.int) int {
fmt.Println("show msg in go ",C.int(keyCode))
//defer C.free(unsafe.Pointer(keyCode)) // will destruct in c
return 1
}
1 change: 1 addition & 0 deletions eventTest/output/eventTest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ELF
Loading