Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
11.9k stars 578 forks source link

How low can you go? Hammerpoon + Parallels #1796

Open carlca opened 6 years ago

carlca commented 6 years ago

Apple Mac Pro (early 2009) 4.1 -> 5.1 firmware upgraded macOS 10.13.5 Parallels 13 for Apple Mac Pop!OS 17.10 + Gnome

Hello Chris, I am trying to get Parallels 13 and Pop!OS (a Ubuntu derived Linux) working on my Apple Mac and, almost inevitably, I am running up against keyboard layout issues. Sadly, the Gnome/Linux utilities don't seem to work well with my Apple keyboard, so I'm wondering if I can solve this in a "low level" manner from the macOS side, using Hammerspoon. Specifically what I want to do is to map my keyboard so that when I press kbd>@</kbd and ", I get @ and ", and likewise when I press ~ (tilde) and ` (backtick), I get ~ and `, that kind of thing. Is Hammerspoon sufficiently low-level to bridge the gap via Parallels into the murky world of Linux, or should I maybe consider an even more low-level solution such as Karabiner-Elements (I read somewhere that Tekezo has released a couple of updates since last year, so I feel confident of avoiding last year's misfortunes!). Thanks in advance, Carl

asmagill commented 6 years ago

Does Parallels see the keyboard as an apple device or as an emulated one? I haven't worked with Parallels in years, but when I boot my mac into Linux directly, I often get the key switching between the characters you mention; within Parallels, type lsmod | grep hid_apple -- if it returns any matched lines, try the following (also in Linux, will have to be root): echo "0" > /sys/module/hid_apple/parameters/iso_layout

If this fixes the keyboard layout, you can make it happen on boot by creating the file /etc/modprobe.d/hid_apple.conf and putting options hid_apple iso_layout=0 in it.

If you don't have the hid_apple module loaded in Parallels, look for another hid or key related module and see what parameters or options it offers by using modinfo.

carlca commented 6 years ago

Hello A-Ron,

“lsmod | grep hid_apple” didn’t return anything, so I guess that takes me on to your third paragraph. At this point, sadly, my Linux skills run out. How exactly should I do what you suggest.

With apologies for my newbieness, Carl Caulkett

On 18 May 2018, 15:50 +0100, A-Ron notifications@github.com, wrote:

Does Parallels see the keyboard as an apple device or as an emulated one? I haven't worked with Parallels in years, but when I boot my mac into Linux directly, I often get the key switching between the characters you mention; within Parallels, type lsmod | grep hid_apple -- if it returns any matched lines, try the following (also in Linux, will have to be root): echo "0" > /sys/module/hid_apple/parameters/iso_layout If this fixes the keyboard layout, you can make it happen on boot by creating the file /etc/modprobe.d/hid_apple.conf and putting options hid_apple iso_layout=0 in it. If you don't have the hid_apple module loaded in Parallels, look for another hid or key related module and see what parameters or options it offers by using modinfo. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

asmagill commented 6 years ago

Sorry for the delay in responding... I'm traveling at the moment so my online access is constrained.

A quick googling of "keyboard map parallels linux" and the like produced some links showing that this has been an issue off and on with Parallels, especially when the mac os is not using the standard us layout (but in a few cases even then)... in a few cases it even required a fix from the Parallels development team but most of those seem to be from about 2013 or older.

I don't use parallels so I'm not sure, but is there a keyboard settings section of the preferences? If so, make sure it matches your Mac OS keyboard layout as selected in System Preferences -> Keyboard -> Input Sources.

On the linux side, since parallels doesn't seem to be passing through the keyboard as a raw device, there probably isn't a relevant module as I suggested above... the next place to check is the keyboard layout as it's defined for either the console or X... do you use a graphical interface in your vm or does it boot to a text console?

If it's a graphical user environment, you may be able to change the keyboard layout on a per user basis by checking the settings for the keyboard or locale (sometimes referred to as "International Settings", depending upon the environment)... the specifics will vary depending upon what window manager you're using (Gnome, KDE, OpenBox, LX, etc.) so your best bet is to google them and see as most of these GUI's are well documented.

To adjust it system wide or if you're using linux through the text console only, it will depend upon the distribution... I'm most familiar with Arch Linux as that's mostly what I use, and https://wiki.archlinux.org/index.php/Keyboard_configuration_in_console gives some details on what you might try. If you're using Debian or variant, try checking out man keyboard and man setupcon when logged into the VM. For other distributions, you might need to google for the details as to what files to edit or commands to use.

Also note that when making changes system wide, you may need to reboot the VM for the changes to take effect.

If it helps in suggesting specific layouts to try, the specific keys that you mention being mapped incorrectly suggest to me that either the mac is set to an international or UK layout and linux is using the US one, or vice versa; this is guess based on limited data, so I could be wrong, but those are the two layouts I'd first try setting the Linux Console to to see what, if anything, changes.

As to whether or not Hammerspoon could do the remapping at a higher level, I'm going to suspect not (or at least not reliably)... When I've used Oracle's Virtual Box (an open source - as much as anything Oracle touches can be - alternative to Parallels and the like) I've found that hammerspoon's mapped hotkeys are inconsistent... I think the capturing of the keyboard does something at an even lower level then we can get at with Hammerspoon.

If you really want a mac side solution, you might check out Karabiner-Elements at https://pqrs.org/osx/karabiner/. I've never used this program myself, but others who use Hammerspoon have found that there are some things Karabiner can do that Hammerspoon can't directly because it includes a lower level kernel extension that can capture key events before Hammerspoon does... I don't know if it will do the kind of remapping you want or not, but if you search for Karabiner in the issues of the Hammerspoon repository you can find some of the things people have found it useful for.

carlca commented 6 years ago

Hello A-Ron,

I decided to try Karabiner and straight away I was delighted to find that it offered a patch which mapped the Cmd key to the Ctrl key under virtual machine, so that was a good start. Unfortunately the documentation is slightly lacking so I’ve posted an issue at their Github site so hopefully I’ll hear something soon.

Thank for the help, Cheers, Carl On 22 May 2018, 08:03 +0100, A-Ron notifications@github.com, wrote:

Sorry for the delay in responding... I'm traveling at the moment so my online access is constrained. A quick googling of "keyboard map parallels linux" and the like produced some links showing that this has been an issue off and on with Parallels, especially when the mac os is not using the standard us layout (but in a few cases even then)... in a few cases it even required a fix from the Parallels development team but most of those seem to be from about 2013 or older. I don't use parallels so I'm not sure, but is there a keyboard settings section of the preferences? If so, make sure it matches your Mac OS keyboard layout as selected in System Preferences -> Keyboard -> Input Sources. On the linux side, since parallels doesn't seem to be passing through the keyboard as a raw device, there probably isn't a relevant module as I suggested above... the next place to check is the keyboard layout as it's defined for either the console or X... do you use a graphical interface in your vm or does it boot to a text console? If it's a graphical user environment, you may be able to change the keyboard layout on a per user basis by checking the settings for the keyboard or locale (sometimes referred to as "International Settings", depending upon the environment)... the specifics will vary depending upon what window manager you're using (Gnome, KDE, OpenBox, LX, etc.) so your best bet is to google them and see as most of these GUI's are well documented. To adjust it system wide or if you're using linux through the text console only, it will depend upon the distribution... I'm most familiar with Arch Linux as that's mostly what I use, and https://wiki.archlinux.org/index.php/Keyboard_configuration_in_console gives some details on what you might try. If you're using Debian or variant, try checking out man keyboard and man setupcon when logged into the VM. For other distributions, you might need to google for the details as to what files to edit or commands to use. Also note that when making changes system wide, you may need to reboot the VM for the changes to take effect. If it helps in suggesting specific layouts to try, the specific keys that you mention being mapped incorrectly suggest to me that either the mac is set to an international or UK layout and linux is using the US one, or vice versa; this is guess based on limited data, so I could be wrong, but those are the two layouts I'd first try setting the Linux Console to to see what, if anything, changes. As to whether or not Hammerspoon could do the remapping at a higher level, I'm going to suspect not (or at least not reliably)... When I've used Oracle's Virtual Box (an open source - as much as anything Oracle touches can be - alternative to Parallels and the like) I've found that hammerspoon's mapped hotkeys are inconsistent... I think the capturing of the keyboard does something at an even lower level then we can get at with Hammerspoon. If you really want a mac side solution, you might check out Karabiner-Elements at https://pqrs.org/osx/karabiner/. I've never used this program myself, but others who use Hammerspoon have found that there are some things Karabiner can do that Hammerspoon can't directly because it includes a lower level kernel extension that can capture key events before Hammerspoon does... I don't know if it will do the kind of remapping you want or not, but if you search for Karabiner in the issues of the Hammerspoon repository you can find some of the things people have found it useful for. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

carlca commented 6 years ago

Although the Karabiner website offers lots of option that one can import into Karabiner, it doesn’t seem to offer an easy way to alter the settings. I’ve found the karabiner.json config file which seems to control everything, so I’m just psyching myself up for an attempt at hacking it, after backing it up, of course!

Cheers, Carl On 28 May 2018, 11:31 +0100, Carl Caulkett carl.caulkett@gmail.com, wrote:

Hello A-Ron,

I decided to try Karabiner and straight away I was delighted to find that it offered a patch which mapped the Cmd key to the Ctrl key under virtual machine, so that was a good start. Unfortunately the documentation is slightly lacking so I’ve posted an issue at their Github site so hopefully I’ll hear something soon.

Thank for the help, Cheers, Carl On 22 May 2018, 08:03 +0100, A-Ron notifications@github.com, wrote:

Sorry for the delay in responding... I'm traveling at the moment so my online access is constrained. A quick googling of "keyboard map parallels linux" and the like produced some links showing that this has been an issue off and on with Parallels, especially when the mac os is not using the standard us layout (but in a few cases even then)... in a few cases it even required a fix from the Parallels development team but most of those seem to be from about 2013 or older. I don't use parallels so I'm not sure, but is there a keyboard settings section of the preferences? If so, make sure it matches your Mac OS keyboard layout as selected in System Preferences -> Keyboard -> Input Sources. On the linux side, since parallels doesn't seem to be passing through the keyboard as a raw device, there probably isn't a relevant module as I suggested above... the next place to check is the keyboard layout as it's defined for either the console or X... do you use a graphical interface in your vm or does it boot to a text console? If it's a graphical user environment, you may be able to change the keyboard layout on a per user basis by checking the settings for the keyboard or locale (sometimes referred to as "International Settings", depending upon the environment)... the specifics will vary depending upon what window manager you're using (Gnome, KDE, OpenBox, LX, etc.) so your best bet is to google them and see as most of these GUI's are well documented. To adjust it system wide or if you're using linux through the text console only, it will depend upon the distribution... I'm most familiar with Arch Linux as that's mostly what I use, and https://wiki.archlinux.org/index.php/Keyboard_configuration_in_console gives some details on what you might try. If you're using Debian or variant, try checking out man keyboard and man setupcon when logged into the VM. For other distributions, you might need to google for the details as to what files to edit or commands to use. Also note that when making changes system wide, you may need to reboot the VM for the changes to take effect. If it helps in suggesting specific layouts to try, the specific keys that you mention being mapped incorrectly suggest to me that either the mac is set to an international or UK layout and linux is using the US one, or vice versa; this is guess based on limited data, so I could be wrong, but those are the two layouts I'd first try setting the Linux Console to to see what, if anything, changes. As to whether or not Hammerspoon could do the remapping at a higher level, I'm going to suspect not (or at least not reliably)... When I've used Oracle's Virtual Box (an open source - as much as anything Oracle touches can be - alternative to Parallels and the like) I've found that hammerspoon's mapped hotkeys are inconsistent... I think the capturing of the keyboard does something at an even lower level then we can get at with Hammerspoon. If you really want a mac side solution, you might check out Karabiner-Elements at https://pqrs.org/osx/karabiner/. I've never used this program myself, but others who use Hammerspoon have found that there are some things Karabiner can do that Hammerspoon can't directly because it includes a lower level kernel extension that can capture key events before Hammerspoon does... I don't know if it will do the kind of remapping you want or not, but if you search for Karabiner in the issues of the Hammerspoon repository you can find some of the things people have found it useful for. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

cmsj commented 6 years ago

@carlca have you set the keymap in Linux to a Mac one? Also note that in Parallels you can edit the Keyboard "hardware" for a VM and map shortcuts directly there. I would try to solve this with Linux and Parallels before getting too hardcore with Karabiner!

carlca commented 6 years ago

Hi Chris, I tried to adjust the settings in Pop!76 (which is a Ubuntu derived bistro running Gnome), and I even tried running Gnome Tweak but no settings appeared to work properly.

As regards the Parallels settings, I was in dialogue with Parallels support and they kept on asking me to solve the problem in Linux rather than with Parallels. That did rather put me off the latter action.

I then installed Karabiner. I started with importing one of the advanced mappings which aimed to swap the Ctrl and the Cmd only while in a virtual machine. It worked perfectly.

Then something strange happened. I received a minor application update to Parallels. Now when I run Pop!OS, the key mappings are just as I want them. means and ~ means ~. I still need to sort out the £ sign, but that’s not so important.

I take your point about not being too ambitious and keeping things simple, but I am now belatedly a huge Karabiner fan. The only downside is that for the Complex Modifications, unless there is an existing suitable temple to install, one has to get into the mysteries of the karabiner.json file. I gather that with the original Karabiner application, the config was a bit simpler. Hopefully Tekezo will improve this one of thee days.

Cheers, Carl On 7 Jun 2018, 20:48 +0100, Chris Jones notifications@github.com, wrote:

@carlca have you set the keymap in Linux to a Mac one? Also note that in Parallels you can edit the Keyboard "hardware" for a VM and map shortcuts directly there. I would try to solve this with Linux and Parallels before getting too hardcore with Karabiner! — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.