Skip to content

Examples

pickpj edited this page Jan 31, 2024 · 6 revisions

Adjusting brightness w/ 1 key

Assumes you have

  • evdev_write function ("Send keystrokes" example below)
  • Declared KEY_BRIGHTNESSDOWN and KEY_BRIGHTNESSUP

Key 665 (when numlock is off) can:

  • Holding lowers brightness
  • Tap and hold Raises brightness

run_hold vv

        case 665:
            if (!nlstate) {
                if (!strcmp(pattern, "1")){
                    evdev_write(KEY_BRIGHTNESSDOWN);
                } else if (len > 1) {
                    evdev_write(KEY_BRIGHTNESSUP);
                }
            }
            break;

Media controls

My Music/Media Control setup (using playerctl)

keys 163/165 can:

  • fastfwd/rewind on hold.
  • next/prev song
  • toggle shuffle/loop with 01

key 164 can:

  • play-pause
  • hold alternates between play/pause

key 74:

  • adjust audio levels of media player
  • 0 for low, 1 for med, len(2) for high, else max

run_hold vv

    switch (keyc) {
        case 164:
            system("playerctl --player=spotify,youtube-music,cmus,%%any play-pause");
            memset(pattern, 0, sizeof(pattern));
            break;
        case 163:
            system("playerctl --player=spotify,youtube-music,cmus,%%any position 5+");
            memset(pattern, 0, sizeof(pattern));
            break;
        case 165:
            system("playerctl --player=spotify,youtube-music,cmus,%%any position 5-");
            memset(pattern, 0, sizeof(pattern));
            break;
    }

run_hold ^^

run_pattern vv

        
    switch (keyc) {
        case 164:
            system("playerctl --player=spotify,youtube-music,cmus,%%any play-pause");
            break;
        case 163:
            if (strcmp(pattern, "01") == 0){
                system("playerctl --player=spotify,youtube-music,cmus,%%any loop | grep Track && playerctl \
                --player=spotify,youtube-music,cmus,%%any loop None || playerctl --player=spotify,youtube-music,cmus,%%any loop Track");
            } else {
                for(int i = 0; i < len; i++) {
                    system("playerctl --player=spotify,youtube-music,cmus,%%any next");
                }
            }
            break;
        case 165:
            if (strcmp(pattern, "01") == 0){
                system("playerctl --player=spotify,youtube-music,cmus,%%any shuffle Toggle");
            } else {
                for(int i = 0; i < len; i++) {
                    system("playerctl --player=spotify,youtube-music,cmus,%%any previous");
                } 
            }
            break;
        case 74:
            if (strcmp(pattern,"0") == 0){
                system("playerctl -p spotify volume 0.5 || playerctl -p youtube-music volume 0.05 || playerctl --player=cmus,%%any volume 0.5");
            } else if (strcmp(pattern,"1") == 0) {
                system("playerctl -p spotify volume 0.7 || playerctl -p youtube-music volume 0.2 || playerctl --player=cmus,%%any volume 0.7");
            } else if (len == 2){
                system("playerctl -p spotify volume 1 || playerctl -p youtube-music volume 0.51 || playerctl --player=cmus,%%any volume 1");
            } else {
                system("playerctl -p spotify volume 1 || playerctl -p youtube-music volume 1 || playerctl --player=cmus,%%any volume 1");
            }
            break;
        }

Send keystrokes

Sending keystrokes without the need for xdotool.
The key needs to be defined first (with libevdev_enable_event_code(dev, EV_KEY, KEY_~~~, NULL);)
Numlock is already defined so just copy this line

static void evdev_write(int keyp) {
    libevdev_uinput_write_event (uidev, EV_KEY, keyp, 1);
    libevdev_uinput_write_event (uidev, EV_SYN, SYN_REPORT, 0);
    libevdev_uinput_write_event (uidev, EV_KEY, keyp, 0);
    libevdev_uinput_write_event (uidev, EV_SYN, SYN_REPORT, 0);
}

With the above function, sending a (volume up) keystroke can be done with evdev_write(EV_VOLUMEUP);

Adding a Modifier to the key press would look like the following
This example presses the grave key while the shift is held down for a tilde ~

    libevdev_uinput_write_event (uidev, EV_KEY, KEY_LEFTSHIFT, 1);
    libevdev_uinput_write_event (uidev, EV_SYN, SYN_REPORT, 0);
    libevdev_uinput_write_event (uidev, EV_KEY, KEY_GRAVE, 1);
    libevdev_uinput_write_event (uidev, EV_SYN, SYN_REPORT, 0);
    libevdev_uinput_write_event (uidev, EV_KEY, KEY_GRAVE, 0);
    libevdev_uinput_write_event (uidev, EV_SYN, SYN_REPORT, 0);
    libevdev_uinput_write_event (uidev, EV_KEY, KEY_LEFTSHIFT, 0);
    libevdev_uinput_write_event (uidev, EV_SYN, SYN_REPORT, 0);

Mouse click

Send left click to position (w/ xdotool)
* Uses regex to parse output of xdotool getmouselocation, which is why it's so bulky *

FILE* fp;
char mpos[50];
regex_t regex;
int reti;
const char *mregex = "x:([0-9]+) y:([0-9]+)";
size_t nmatch = 3;
regmatch_t pmatch[3];


// ^^ Place above the function ^^

        case 65:
            fp = popen("xdotool getmouselocation", "r");
            fgets(mpos, sizeof(mpos)-1, fp);
            reti = regcomp(&regex, mregex, REG_EXTENDED);
            reti = regexec(&regex, mpos, nmatch, pmatch, 0);
            pclose(fp);
            system("xdotool mousemove 1900 1050 && xdotool click 1");
            sprintf(pattern, "xdotool mousemove %.*s %.*s", (int)(pmatch[1].rm_eo - pmatch[1].rm_so), mpos + pmatch[1].rm_so, (int)(pmatch[2].rm_eo - pmatch[2].rm_so), mpos + pmatch[2].rm_so);
            system(pattern);
            regfree(&regex);
            break;

More examples

Can be found in the Examples Branch
These are full examples with more aspects altered.

Clone this wiki locally