Keymaps - how to identify the key pressed


#1

when the method is called the jquery.Event doesn’t have ‘which’/keyPress field …The use case is when multiple keymaps are associated with one method is there a way to identify which key combination was pressed to get to the method.


#2

Have you tried using the Keybinding Resolver (Cmd+. on OS X and Ctrl+. on other platforms)? It will show you what key Atom saw you press and the command that was triggered because of it.


#3

to clarify this is for the custom package and the requirement is to identify programatically the key pressed. if there are multiple keymaps to the same package method…when the method is called, the arguments passed has jquery.Event but it doesn’t have the keyPress/which to identify which key combination was pressed to get to the method


#4

Why would you have multiple keybindings for one command in your own package? And if you did, why would you need to do something different based on which keybinding is pressed?

For example, in the fuzzy-finder package there are two keybindings for the file picker, Cmd+P and Cmd+T. But they both open the file picker, so you don’t need to know which keybinding is pressed. If a keybinding is supposed to do something different … then map it to a different command. And then you don’t need to know which keybinding is pressed.


#5

if there is a logic around the key combination pressed …what is the way…why wouldn’t the jquery.Event not pass the key pressed…


#6

I don’t know, I’ve never needed to do what you are suggesting. If you could fill in what you’re actually trying to accomplish, I might be able to suggest an easier way of getting it done.


#7

@leedohm I managed to figure out the dynamic key press combination. The purpose is navigate package (heads up --> you need browser-plus package to try this). This allows the users to dynamically set the url in the config file to which to navigate to. the default set up in config right now is

‘F1’:
title: 'F1 - Help’
type: 'string’
default: ‘http://devdocs.io/#q=&searchterm

‘CTRL-F1’:
title: 'F1 - Help’
type: 'string’
default: ‘https://www.google.com/search?q=&searchterm

‘CTRL-F2’:
title: 'Stack Overflow Search’
type: 'string’
default: ‘http://stackoverflow.com/search?q=&searchterm

‘CTRL-F3’:
title: 'AtomIO Search’
type: 'string’
default: ‘https://atom.io/docs/api/search/latest?q=&searchterm

what this does is …you can press F1 to go the the help using devdocs and ctrl-f1 would take to google help for the word under and stack overflow(ctrl-f2) and atom(ctrl-f3) and the good thing is you can add your own key with a different url…I think this really makes ATOM coool.

The thing right now…I got a issue logged in the github saying the event is not available in the linux OS…I am using the

evt.originalEvent.keyIdentifier for capturing the key combination…I have tested this in window…wondering if you/ anybody can throw some light on this…


#8

Why can’t you have something like this?

# lib/my-package.coffee
class MyPackage
  activate: ->
    atom.commands.add 'my-package:ctrl-f1', @ctrlF1
    atom.commands.add 'my-package:ctrl-f2', @ctrlF2
    # etc...

  ctrlF1: (e) =>
    @actualFunction 'f1', e

  ctrlF2: (e) =>
    @actualFunction 'f2', e

  # etc...

  actualFunction: (keyPressed, e) ->
    # do something based on the key pressed
# keymaps/my-package.cson
'atom-workspace':
  'ctrl-f1': 'my-package:ctrl-f1'
  'ctrl-f2': 'my-package:ctrl-f2'
  # etc...

#9

That is exactly the point of dynamic. You can add your key combination say ctrl-f4 with a different URL . The key combination can be added keymap.cson and the corresponding URL maintained in configuration.coffee in the package.
And it will start to work!!

BTW Were you able to test it.