Skip to content

Remap a keyboard key as a left mouse button unable to register a double click #176

@dedisuryadi

Description

@dedisuryadi

Hello,

First, I want to say thank you for this library. I'm currently trying to remap a button (the F20 key in my case) to mimic the behavior of a left mouse button. It's working wonderfully; I can click, block, drag, and drop. However, I'm facing an issue with dispatching double clicks (no text is selected if I try to double click on a text).

Could anyone provide any pointers on how to achieve this? By the way, I'm using an M1 Mac running Monterey, and I'm unable to use Karabiner-Elements due to a specific reason. I've attached the dispatch function for reference.

Thank you very much

void dispatch_proc(uiohook_event * const event) {
    mev = (uiohook_event *) malloc(sizeof(uiohook_event));
    mev->data.mouse.x = x;
    mev->data.mouse.y = y;
    mev->data.mouse.button = MOUSE_BUTTON1;

    // Grab the event timestamp for use later
    uint64_t timestamp = (uint64_t) event->time;

    switch (event->type) {
        case EVENT_KEY_PRESSED:
            if (event->data.keyboard.keycode == VC_F20) {
                event->reserved = 0x1; // stop F20 propagation

                // Track the number of clicks.
                if (ispressed == 0 && (long int) (timestamp - click_time) / 1000000 <= hook_get_multi_click_time()) {
                    if (click_count < USHRT_MAX) {
                        click_count++;
                    }
                } else {
                    // Reset the click count.
                    click_count = 1;
                }

                // Save this events time to calculate the click_count.
                click_time = timestamp;

                if (ispressed == 0) {
                    mev->type = EVENT_MOUSE_PRESSED;
                    fprintf(stdout, "%s - %d\n", "key as pressed", click_count);
                } else {
                    mev->type = EVENT_MOUSE_MOVED;
                    mev->data.mouse.button = MOUSE_NOBUTTON;
                    fprintf(stdout, "%s - %d\n", "key as moved", click_count);
                };

                ispressed = 1;
                hook_post_event(mev);
            }
            break;

        case EVENT_KEY_RELEASED:
            if (event->data.keyboard.keycode == VC_F20) {
                event->reserved = 0x1; // stop F20 propagation

                if ((long int) (timestamp - click_time) / 1000000 > hook_get_multi_click_time()) {
                    if (click_count > 1) {
                        mev->type = EVENT_MOUSE_CLICKED;
                        mev->data.mouse.clicks = click_count;
                        hook_post_event(mev);
                    }

                    // Reset the click count.
                    click_count = 0;

                } else {
                    mev->type = EVENT_MOUSE_RELEASED;
                    fprintf(stdout, "%s - %d\n", "key released", click_count);

                    hook_post_event(mev);
                    ispressed = 0;
                }
            }
            break;

        case EVENT_MOUSE_PRESSED:
        case EVENT_MOUSE_RELEASED:
        case EVENT_MOUSE_CLICKED:
            fprintf(stdout, "%s - %d\n", "got clicked", event->data.mouse.clicks);
        case EVENT_MOUSE_MOVED:
        case EVENT_MOUSE_WHEEL:
        case EVENT_MOUSE_DRAGGED:
                x = event->data.mouse.x;
                y = event->data.mouse.y;
            break;

        default:
            break;
    }

    free(mev);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions