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

Automatically select named profile when keyboard plugged in & unplugged #354

Closed
matthewadams opened this issue Feb 26, 2015 · 6 comments

Comments

@matthewadams
Copy link

commented Feb 26, 2015

I have two profiles: "Mac" (the default) and "Win" (for when I plug in my Microsoft Natural keyboard). It would be nice if Karabiner automatically selected my "Win" profile when my Microsoft Natural keyboard is plugged in (via USB), and revert to my "Mac" profile when an external keyboard is no longer present.

More generally, allow a user to specify which profile is the default to be used when no external keyboard is present, and let the user specify which profile to switch to when a given keyboard is plugged in.

@tekezo

This comment has been minimized.

Copy link
Owner

commented Feb 27, 2015

Thank you for feedback.
I have a plan to do it by another project that triggers commands by system events (device plugged/unplugged, app switching, input source switching.)

@mwq27

This comment has been minimized.

Copy link

commented Jun 2, 2015

What's the name of the new project with this feature?

@tekezo

This comment has been minimized.

Copy link
Owner

commented Jun 3, 2015

Hammerspoon has been support this feature.
So, I don't need to launch new project.

Please try usb.watcher.
http://www.hammerspoon.org/

@tekezo tekezo closed this Sep 16, 2015

@gabamnml

This comment has been minimized.

Copy link

commented Oct 18, 2015

I did it. Here is the code

local usbWatcher = nil

function usbDeviceCallback(data)
    if (data["productName"] == "USB Keyboard") then
        if (data["eventType"] == "added") then
            hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 1')
        elseif (data["eventType"] == "removed") then
            hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 0')
        end
    end
end

usbWatcher = hs.usb.watcher.new(usbDeviceCallback)
usbWatcher:start()

0 is my 'default' profile
1 is for my USB keyboard modified layout

@neooleg

This comment has been minimized.

Copy link

commented Oct 25, 2016

This is solution for switching device which is absolutely cool.

But what about applications switching -- in particular VM? In Karabiner there is amazing VIRTUALMACHINE appdef which matches plenty VMs and apps in seamless mode. However, AFAIK http://www.hammerspoon.org/ allows to match by app name only.

So, is there is a way to switch profile per appdef, e.g. VIRTUALMACHINE (ideally w/o 3rd party tools)?

Thanks!

@jfinney122

This comment has been minimized.

Copy link

commented Dec 14, 2016

@gabamnml, thanks for the example script. I took what you did and extended it a little further by initially checking all usb devices to see if the keyboard is plugged in when Hammerspoon starts. I put this script in my init.lua and set Hammerspoon to run on boot up. That way whether my keyboard is plugged in or not, it will select the correct profile without a device having to be connected or disconnected. Otherwise the keyboard could be connected when initally starting the machine and the default profile would be selected, not the keyboard profile

hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 0')

local usb_table = nil

usb_table = hs.usb.attachedDevices()

for index, usb_device in pairs(usb_table) do
	if (usb_device["productName"] == "USB-HID Keyboard") then
		hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 1')
	end
end

local usbWatcher = nil

function usbDeviceCallback(data)
    if (data["productName"] == "USB-HID Keyboard") then
        if (data["eventType"] == "added") then
            hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 1')
        elseif (data["eventType"] == "removed") then
            hs.execute('/Applications/Karabiner.app/Contents/Library/bin/karabiner select 0')
        end
    end
end

usbWatcher = hs.usb.watcher.new(usbDeviceCallback)
usbWatcher:start()

In my case, the USB Keyboard is named "USB-HID Keyboard". I'm using a Ducky Shine 5 on my Mac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.