pulsar-edit / pulsar

A Community-led Hyper-Hackable Text Editor
https://pulsar-edit.dev
Other
3.23k stars 137 forks source link

Enable Native Double Tap as Keybinding (keymap.cson) #301

Open rajmondx opened 1 year ago

rajmondx commented 1 year ago

Have you checked for existing feature requests?

Summary

Allow users to double tap any button (eg. as IDEA allows it with their "double shift") something like this

'.platform-win32':
  'shift-shift': 'fuzzy-finder:toggle-file-finder'

# or plus
'.platform-win32':
  'shift+shift': 'fuzzy-finder:toggle-file-finder'

What benefits does this feature provide?

No big deal. I would just love to see this feature because of the muscle memory gained in IDEA, hard to not double press shift and somehow I believe some idea users might not like any ide if its not possible to configure double tap.

Any alternatives?

Edit File > Init Script (init.js):

const keystrokeCache = {};
const maxDoubleTime = 500;
const allowKeystrokeResolverOnlyFor = ['shift'/*, 'enter'*/];

atom.keymaps.addKeystrokeResolver((keypress)=>{
  try{
    const keystroke = keypress.keystroke;
    if(allowKeystrokeResolverOnlyFor.indexOf(keystroke) >= 0){
      // keypress.event.repeat == is holding, doesnt count
      if(!keypress.event.repeat && keypress.event.type == 'keydown'){
        const lastPressedTimestamp = keystrokeCache[keystroke] || keypress.event.timeStamp;
        const currentPressedTimestamp = keypress.event.timeStamp;
        const diff = currentPressedTimestamp-lastPressedTimestamp;
        if(diff > 0 && diff < maxDoubleTime){
          return `${keystroke}+${keystroke}`;
        }
        keystrokeCache[keystroke] = keypress.event.timeStamp;
      }
    }
  }catch(e){
    console.error('DoubleKeystrokeResolver: ', e);
  }
});

Use in File > Keymap (keymap.cson) for example:

# Use + instead of -
'.platform-win32':
  'shift+shift': 'fuzzy-finder:toggle-file-finder'

hint. Check allowKeystrokeResolverOnlyFor to allow any other buttons than shift for double tap (be caution with buttons like backspace)

hint. You need to use + instead of - in keymap.cson because - doesn't work for double tap (it seems that pulsar-edit is interpreting shift-shift like a single button press).

Other examples:

No response

confused-Techie commented 1 year ago

Thanks for the contribution, this would be a pretty cool feature to see integrated

rajmondx commented 1 year ago

Thanks for the contribution, this would be a pretty cool feature to see integrated

Check "Alternatives": I wrote a script which you could add to your own init.js, its a bit hacky and it could be done better (eg. if your init.js gets bigger such "little scripts" end up messy or you could increase the performance by using keyid instead of strings etc.) but it does what its supposed to do.

mauricioszabo commented 1 year ago

I don't think this is supported by Electron or browsers in general (trigger an event when a modifier key is pressed). If they don't send us the keystroke, there's nothing we can actually do...

One thing they you CAN do already is double tap any key that's not a modifier. For example, the keystroke 'esc esc' works today (double tap escape key).