How to make mac go to sleep with holding down a key with Karabiner?

555 views Asked by At

I'd like to make a complex modification that makes macOS to go to sleep when I holding the F6/☾ key.

The following config is working BUT when I release the key that wakes up the OS instantly.

Maybe it can be combine with to_after_key_up but can't find it how to do it.

{
    "description": "Map F6 (Do Not Disturb) to Cmd+Opt+Eject for Sleep Mac",
    "manipulators": [{
            "from": {
                "key_code": "f6"
            },
            "type": "basic",
            "parameters": {
                "basic.to_if_alone_timeout_milliseconds": 250,
                "basic.to_if_held_down_threshold_milliseconds": 1000
            },
            "to_if_alone": [
                {
                    "key_code": "f6"
                }
            ],
            "to_if_held_down": [
                {
                    "repeat": false,
                    "consumer_key_code": "eject",
                    "modifiers": ["left_gui", "left_alt"]
                }
            ]
    }]
}
1

There are 1 answers

0
erkage On BEST ANSWER

Can't find a solution to the key releasing issue so as a workaround I'm created one with double press the F6/☾ key for same functionality:

{
    "description": "Map 2x F6 (Do Not Disturb) to Cmd+Opt+Eject for Sleep Mac",
    "manipulators": [
            {
                "type": "basic",
                "conditions": [
                    {
                            "type": "variable_if",
                            "name": "sleep",
                            "value": 1
                    }
                ],
                "from": {
                    "key_code": "f6"
                },
                "to_after_key_up": [{
                    "repeat": false,
                    "consumer_key_code": "eject",
                    "modifiers": ["left_gui", "left_alt"]
                }]
            },
            {
                "type": "basic",
                "from": {
                    "key_code": "f6"
                },
                "to": [
                    {
                            "set_variable": {
                                "name": "sleep",
                                "value": 1
                            }
                    }
                ],
                "to_delayed_action": {
                    "to_if_invoked": [
                            {
                                "set_variable": {
                                    "name": "sleep",
                                    "value": 0
                                }
                            },
                            {
                                "key_code": "f6"
                            }
                    ],
                    "to_if_canceled": [
                            {
                                "set_variable": {
                                    "name": "sleep",
                                    "value": 0
                                }
                            }
                    ]
                },
                "parameters": {
                    "basic.to_delayed_action_delay_milliseconds": 200
                }
            }
    ]
}

The reason I wanted to differentiate against single F6 press is that I'm using it in file explorers to move files between panels.