Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Applications > Quick (one-click) way to swap Ctrl and Command keys?

Quick (one-click) way to swap Ctrl and Command keys?
Thread Tools
papercup12
Fresh-Faced Recruit
Join Date: Jul 2009
Status: Offline
Reply With Quote
Jul 10, 2009, 12:25 PM
 
I see that I can swap the Control and Command keys using "System Prefs --> Keyboard & Mouse --> Keyboard (tab) --> Modifier Keys ...", however that takes multiple clicks to do. I need to be able to swap the keys back and forth multiple times an hour while working. So, ideally this would be either a single click or else a shell script (I usually only need the keys swapped while using the Terminal).

Any suggestions?

I don't know Applescript nor do I know how to use the "Automator" app. I tried recording an applescript while doing the key swap, but it didn't seem to want to record those events.
     
mduell
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status: Offline
Reply With Quote
Jul 10, 2009, 03:17 PM
 
I don't have a solution, but I'd love to know why.
     
CharlesS
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Jul 10, 2009, 03:48 PM
 
Here you go: it's clunky, and requires "Enable Access for Assistive Devices" to be turned on in the Universal Access prefs, but it should work:

Code:
tell application "System Events" set needToWait to not (exists process "System Preferences") tell application "System Preferences" activate end tell if needToWait is true then delay 1 tell process "System Preferences" click menu item "Keyboard & Mouse" of menu "View" of menu bar 1 tell tab group 1 of window "Keyboard & Mouse" click radio button "Keyboard" click button "Modifier Keys…" end tell set cmd to do shell script "perl -e 'print \"\\x{2318}\"'" set ctrl to do shell script "perl -e 'print \"\\x{2303}\"'" tell sheet 1 of window "Keyboard & Mouse" tell (pop up button ("Command (" & cmd & ") Key:")) click click menu item (ctrl & " Control") of menu 1 end tell delay 0.5 tell pop up button ("Control (" & ctrl & ") Key:") click click menu item (cmd & " Command") of menu 1 end tell delay 0.5 click button "OK" end tell end tell end tell tell application "System Preferences" to quit
To change it back:

Code:
tell application "System Events" set needToWait to not (exists process "System Preferences") tell application "System Preferences" activate end tell if needToWait is true then delay 1 tell process "System Preferences" click menu item "Keyboard & Mouse" of menu "View" of menu bar 1 tell tab group 1 of window "Keyboard & Mouse" click radio button "Keyboard" click button "Modifier Keys…" end tell set cmd to do shell script "perl -e 'print \"\\x{2318}\"'" set ctrl to do shell script "perl -e 'print \"\\x{2303}\"'" tell sheet 1 of window "Keyboard & Mouse" tell pop up button ("Command (" & cmd & ") Key:") click click menu item (cmd & " Command") of menu 1 end tell delay 0.5 tell pop up button ("Control (" & ctrl & ") Key:") click click menu item (ctrl & " Control") of menu 1 end tell delay 0.5 click button "OK" end tell end tell end tell tell application "System Preferences" to quit
( Last edited by CharlesS; Jul 16, 2009 at 03:00 PM. Reason: made the scripts encoding-agnostic)

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
papercup12  (op)
Fresh-Faced Recruit
Join Date: Jul 2009
Status: Offline
Reply With Quote
Jul 10, 2009, 05:31 PM
 
> I don't have a solution, but I'd love to know why.

I've been a GNU/Linux user for a while, and my fingers really like the Control keys right where they are. I use Ctrl an Alt all time there.

I'm now using a Mac quite a bit, and Mac apps use the Command key for most things (just like how the Control key is used for many GUI apps in GNU/Linux).

So, when I'm using GNU/Linux, I want the Control key to be the Control key, but when I'm using Mac OS X, I want the Control key to actually be the Command key.

Now, *that* I can do. But when I use Terminal.app in OS X, I want the Control keys back -- just like with GNU/Linux.

So, what I see as a marginally-tolerable solution is to have some simple script that I can run every time I go to use the Terminal. It's a pain, but if it's only one script, I can just run it every time I go to use the terminal (to swap those keys), and then run it again when I leave (to swap them back).

I"m actually quite happy with only having the Ctrl and Alt keys on GNU/Linux, and running OS X is actually gumming up the works for me since it adds that darned 3rd key. But, currently I need to run OS X for various reasons, so I'm trying to find a way to make it work.
     
papercup12  (op)
Fresh-Faced Recruit
Join Date: Jul 2009
Status: Offline
Reply With Quote
Jul 10, 2009, 05:38 PM
 
CharlesS,

I assume I'm supposed to leave out the "Code:" line from each of those.

1. Do I save those files as UTF-8 (to preserve the clover and control symbols)?

2. What filename extension do I use?

3. How do I run them? Do I need to put a shebang line in them, as in "#!/usr/bin/applescript"? Or do I put them on the desktop and double-click them?

Thanks.
     
turtle777
Clinically Insane
Join Date: Jun 2001
Location: planning a comeback !
Status: Offline
Reply With Quote
Jul 10, 2009, 05:46 PM
 
Depends on what you mean by "run them".

Just open the Apple Script application, paste the code, and save it as a standalone app.

I'm not sure how to automate it with your switch back and forth from Terminal to something else.

-t
     
papercup12  (op)
Fresh-Faced Recruit
Join Date: Jul 2009
Status: Offline
Reply With Quote
Jul 13, 2009, 09:44 AM
 
> Depends on what you mean by "run them".

I'm accustomed to running programs by going into the Terminal and typing their name, possibly followed by arguments and options.

Another way of running programs on Mac OS X is to double-click their icon.

> I'm not sure how to automate it with your switch back and forth from Terminal to something else.

Yeah, I'm beginning to think that there's no easy answer, and that I just need to get used to using the Command key in addition to Ctrl and Alt (which confuses my fingers when I'm using GNU/Linux). CharlesS's solution might work for me (and I'm still hoping he replies to my questions about his post), but it looks like it involves a 1 second delay, and also I'm not sure how it'll work with the OS X feature of remembering modifier key remaps *per keyboard*...

(Turns out there seems to be a bug in the dialog box for this: if you plug in your external keyboard, then remap, then unplug, there's no way to put settings back to normal for your built-in keyboard without plugging the external keyboard in again. Not sure where I'd go to report an OS X bug.)

Thanks.
     
clarkgoble
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Jul 16, 2009, 01:30 PM
 
If you have iKey or Quickeys they both let you associate an Applescript with a key combination of your choosing. (Including the function keys)
     
lpkmckenna
Addicted to MacNN
Join Date: Jul 2004
Location: Toronto
Status: Offline
Reply With Quote
Jul 16, 2009, 01:57 PM
 
FastScripts is free for up to 10 scripts.
     
CharlesS
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Jul 16, 2009, 02:26 PM
 
Originally Posted by papercup12 View Post
Yeah, I'm beginning to think that there's no easy answer, and that I just need to get used to using the Command key in addition to Ctrl and Alt (which confuses my fingers when I'm using GNU/Linux). CharlesS's solution might work for me (and I'm still hoping he replies to my questions about his post), but it looks like it involves a 1 second delay, and also I'm not sure how it'll work with the OS X feature of remembering modifier key remaps *per keyboard*...
Sorry about that, somehow I missed your reply to my post. To answer your questions:

1. All you have to do is open the Script Editor and paste the script in. You shouldn't have to worry about encoding as Script Editor is always Unicode, I think.

EDIT: I just edited the scripts so that they should work in any encoding.

2. If you want to assign the script to a keystroke, there is a donationware application called Butler that can do that.

3. The 1 second delay is to give System Preferences time to open after the script launches it. Otherwise, the script will try to click on stuff when the app's not launched yet, and it won't work. Similarly, the half-second delays later is so the menus can close after the script has clicked on a menu item.

4. I didn't take the per-keyboard settings into account. Did you want to change the settings for only one keyboard?
( Last edited by CharlesS; Jul 16, 2009 at 03:01 PM. )

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
clarkgoble
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Jul 16, 2009, 03:22 PM
 
I should point out that while you only want this one function, having a macro program (any of the above listed ones) to run various scripts is amazingly useful. Investing in a good macro program really is worth it.
     
papercup12  (op)
Fresh-Faced Recruit
Join Date: Jul 2009
Status: Offline
Reply With Quote
Jul 22, 2009, 09:26 AM
 
@ CharlesS: Thanks for the reply. After copy/pasting the code into the upper Script Editor app window, what do you then do? Compile? File Save?

Update though: in the 12 days since starting this thread, I've had to get work done, and so just forced my fingers to learn a new key at a new location (the Command key -- now mapped to caps lock). What this resulted in is basically just having to re-teach my fingers the basics like cut/copy/paste/save/open/new and a few others. I kept Control and Alt where they were (since I need them there for other unix apps that I can't do without).

Regarding #4, the per-keyboard setting doesn't matter, since my hands are only on one keyboard at a time. I mentioned it because of the bug I discovered there.

Thanks also for the link to Butler.

@ clark & lpk: found iKey here Script Software - Essential Mac Automation for Everyone with iKey and QuicKeys here Startly - QuicKeys�4 for Mac OS X . I'm not used to needing a "macro program" to run various scripts, since on GNU/Linux you just Alt-Tab to a terminal window and run them in the usual way. But I can definitely see how choosing some previously-unused modifier key combination as a prefix for running your own selection of utility scripts would be very useful.
     
CharlesS
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Jul 22, 2009, 12:59 PM
 
Originally Posted by papercup12 View Post
@ CharlesS: Thanks for the reply. After copy/pasting the code into the upper Script Editor app window, what do you then do? Compile? File Save?
Run.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
clarkgoble
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Jul 22, 2009, 02:09 PM
 
I actually think they'd be just as useful in Linux. Yes you can go to the Terminal in OSX and type some command. But the nice thing about Macros is that they can (a) manipulate GUI interfaces (b) modify some existing shortcut and (c) are much faster.

There are some simple macro programs for Linux (xmacro) but I'm not aware of anything as robust as iKey or Quickeys.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:41 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,