Skip to content

Instantly share code, notes, and snippets.

@mossprescott
Created December 12, 2014 23:19
Show Gist options
  • Select an option

  • Save mossprescott/3086d6c31debfecd697a to your computer and use it in GitHub Desktop.

Select an option

Save mossprescott/3086d6c31debfecd697a to your computer and use it in GitHub Desktop.
Teaching Swing how text works on the Mac
if (scala.util.Properties.isMac) {
import java.awt.event.KeyEvent._
import javax.swing.KeyStroke.getKeyStroke
val cmd = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
val opt = java.awt.event.InputEvent.ALT_DOWN_MASK
val shift = java.awt.event.InputEvent.SHIFT_DOWN_MASK
val extraKeys = Map(
getKeyStroke(VK_X, cmd) -> "cut-to-clipboard",
getKeyStroke(VK_C, cmd) -> "copy-to-clipboard",
getKeyStroke(VK_V, cmd) -> "paste-from-clipboard",
getKeyStroke(VK_A, cmd) -> "select-all",
getKeyStroke(VK_LEFT, opt) -> "caret-previous-word",
getKeyStroke(VK_KP_LEFT, opt) -> "caret-previous-word",
getKeyStroke(VK_RIGHT, opt) -> "caret-next-word",
getKeyStroke(VK_KP_RIGHT, opt) -> "caret-next-word",
getKeyStroke(VK_LEFT, cmd) -> "caret-begin-line",
getKeyStroke(VK_KP_LEFT, cmd) -> "caret-begin-line",
getKeyStroke(VK_RIGHT, cmd) -> "caret-end-line",
getKeyStroke(VK_KP_RIGHT, cmd) -> "caret-end-line",
getKeyStroke(VK_LEFT, opt + shift) -> "selection-previous-word",
getKeyStroke(VK_KP_LEFT, opt + shift) -> "selection-previous-word",
getKeyStroke(VK_RIGHT, opt + shift) -> "selection-next-word",
getKeyStroke(VK_KP_RIGHT, opt + shift) -> "selection-next-word",
getKeyStroke(VK_LEFT, cmd + shift) -> "selection-begin-line",
getKeyStroke(VK_KP_LEFT, cmd + shift) -> "selection-begin-line",
getKeyStroke(VK_RIGHT, cmd + shift) -> "selection-end-line",
getKeyStroke(VK_KP_RIGHT, cmd + shift) -> "selection-end-line"
)
val im = comp.peer.getInputMap
extraKeys.map { case (k, a) => im.put(k, a) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment