Skip to content
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ list of features
| blacklist applications | [x] |
| use media-keys as hotkey | [x] |
| synthesize a key-press | [x] |
| noremap / key forwarding | [x] |

### Install

Expand Down Expand Up @@ -96,6 +97,7 @@ mode = 'name of mode' | <mode> ',' <mode>
action = <keysym> '[' <proc_map_lst> ']' | <keysym> '->' '[' <proc_map_lst> ']'
<keysym> ':' <command> | <keysym> '->' ':' <command>
<keysym> ';' <mode> | <keysym> '->' ';' <mode>
<keysym> '|' <keysym>

keysym = <mod> '-' <key> | <key>

Expand Down Expand Up @@ -172,3 +174,11 @@ General options that configure the behaviour of **skhd**:
"google chrome"
]
```

Key forwarding (noremap like functionality):

```
# specify source key (left) and target key (right) to synthesize
cmd - 1 : yabai -m space --focus 1
ctrl - 1 | cmd - 1 # press `ctrl - 1` will be `cmd - 1`, bypassing `cmd - 1` command above
```
29 changes: 29 additions & 0 deletions src/hotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,34 @@ find_process_command_mapping(struct hotkey *hotkey, uint32_t *capture, struct ca
return result;
}

bool find_and_forward_hotkey(struct hotkey *k, struct mode *m, CGEventRef event)
{
struct hotkey *found = table_find(&m->hotkey_map, k);
if (!found || !found->forwarded_hotkey) return false;
debug("forwarding hotkey\n");
struct hotkey *forwarded = found->forwarded_hotkey;

int flags = 0;
if (has_flags(forwarded, Hotkey_Flag_Alt)) {
flags |= kCGEventFlagMaskAlternate;
}
if (has_flags(forwarded, Hotkey_Flag_Shift)) {
flags |= kCGEventFlagMaskShift;
}
if (has_flags(forwarded, Hotkey_Flag_Cmd)) {
flags |= kCGEventFlagMaskCommand;
}
if (has_flags(forwarded, Hotkey_Flag_Control)) {
flags |= kCGEventFlagMaskControl;
}
if (has_flags(forwarded, Hotkey_Flag_Fn)) {
flags |= kCGEventFlagMaskSecondaryFn;
}
CGEventSetFlags(event, flags);
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, forwarded->key);
return true;
}

bool find_and_exec_hotkey(struct hotkey *k, struct table *t, struct mode **m, struct carbon_event *carbon)
{
uint32_t c = MODE_CAPTURE((int)(*m)->capture);
Expand Down Expand Up @@ -212,6 +240,7 @@ void free_mode_map(struct table *mode_map)
}
buf_free(hotkey->command);

if (hotkey->forwarded_hotkey) free(hotkey->forwarded_hotkey);
free(hotkey);
next:;
}
Expand Down
1 change: 1 addition & 0 deletions src/hotkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct hotkey
char **command;
char *wildcard_command;
struct mode **mode_list;
struct hotkey *forwarded_hotkey;
};

#define internal static
Expand Down
11 changes: 10 additions & 1 deletion src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,16 @@ parse_hotkey(struct parser *parser)
hotkey->flags |= Hotkey_Flag_Passthrough;
}

if (parser_match(parser, Token_Command)) {
if (parser_match(parser, Token_Forward)) {
debug(" forward key stroke: {\n");
struct hotkey *forwarded = parse_keypress(parser);
if (!forwarded) {
parser_report_error(parser, parser_peek(parser), "expect keysym\n");
goto err;
}
hotkey->forwarded_hotkey = forwarded;
debug(" }\n");
} else if (parser_match(parser, Token_Command)) {
parse_command(parser, hotkey);
} else if (parser_match(parser, Token_BeginList)) {
parse_process_command_list(parser, hotkey);
Expand Down
5 changes: 4 additions & 1 deletion src/skhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ internal EVENT_TAP_CALLBACK(key_handler)
if (table_find(&blacklst, carbon.process_name)) return event;
if (!current_mode) return event;

BEGIN_TIMED_BLOCK("handle_keypress");
struct hotkey eventkey = create_eventkey(event);
if (find_and_forward_hotkey(&eventkey, current_mode, event)) {
return event;
}
BEGIN_TIMED_BLOCK("handle_keypress");
bool result = find_and_exec_hotkey(&eventkey, &mode_map, &current_mode, &carbon);
END_TIMED_BLOCK();

Expand Down
9 changes: 9 additions & 0 deletions src/tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ get_token(struct tokenizer *tokenizer)
token.type = Token_Command;
}
} break;
case '|': {
eat_whitespace(tokenizer);

token.text = tokenizer->at;
token.line = tokenizer->line;
token.cursor = tokenizer->cursor;

token.type = Token_Forward;
} break;
default: {
if (c == '0' && *tokenizer->at == 'x') {
advance(tokenizer);
Expand Down
1 change: 1 addition & 0 deletions src/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum token_type
Token_Key,

Token_Decl,
Token_Forward,
Token_Comma,
Token_Insert,
Token_Plus,
Expand Down