Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable editor shortcuts #6804

Open
iqnite opened this issue Oct 23, 2023 · 21 comments
Open

Disable editor shortcuts #6804

iqnite opened this issue Oct 23, 2023 · 21 comments
Labels
new addon Related to new addons to this extension. `scope: addons` should still be added. scope: addon Related to one or multiple addons type: enhancement New feature for the project

Comments

@iqnite
Copy link
Contributor

iqnite commented Oct 23, 2023

Why this would be helpful

It's extremely frustrating when you press a key in a project and that key affects the editor.
For example, you surely experienced that when you have a selected object in the paint editor and press the arrow keys in the project, the object suddenly shifts around? Yes, that's because every part of the editor reacts to any key press.
Another very well-known thing is when the last block you interacted with gets deleted by pressing Alt+X to pause the project in the editor, as "X" is recognized as binding for deleting.

How this addon works

This addon disables key interactions for areas that are not hovered, as Unity does.

Possible alternatives

To address the Alt+X issue and similar things, we could also consider disabling those on-key bindings for the codespace. I don't know anybody who uses them, and they are annoying as you first need to execute the block by clicking on it.

Additional context

I was working on a game when I noticed a big bug that never occurred before. I looked for half an hour for a patch when I suddenly realized that an if-else block with a lot of nested code had mysteriously disappeared. I had to rebuild it from scratch, and that wasn't fun.
Please don't comment "I use those keybinds!" or stuff like that. Thanks.

@iqnite iqnite added new addon Related to new addons to this extension. `scope: addons` should still be added. scope: addon Related to one or multiple addons type: enhancement New feature for the project labels Oct 23, 2023
@DNin01
Copy link
Member

DNin01 commented Oct 24, 2023

I don't know if maybe clicking on the stage (to focus it) first could prevent this from happening? Edit: it does not!

I've also experienced this problem the other way around - shifting something in the costume editor doing something in the project. Wonder what we could do about that…

Off-topic: Sometimes I use Backspace or Delete to delete blocks, but I did not know that x does too! (Apparently, it's actually Alt+x.)

@DNin01 DNin01 changed the title Disable shortcuts Disable editor shortcuts Oct 25, 2023
@WorldLanguages
Copy link
Member

Off-topic: Sometimes I use Backspace or Delete to delete blocks, but I did not know that x does too! (Apparently, it's actually Alt+x.)

Ugh, and we thought Alt+X for pausing was a unique combination...

@WorldLanguages
Copy link
Member

For example, you surely experienced that when you have a selected object in the paint editor and press the arrow keys in the project, the object suddenly shifts around? Yes, that's because every part of the editor reacts to any key press.

Can anyone link to the relevant scratch-gui files that add the event listeners to the page? I believe we're looking for different event listeners for the code area and for the costume editor. Does the sound editor have keyboard shortcuts?

@DNin01
Copy link
Member

DNin01 commented Oct 25, 2023

Can anyone link to the relevant scratch-gui files that add the event listeners to the page?

Here is where it picks up Alt+X:
Event: scratch-blocks/core/blockly.js#L185 Blockly.onKeyDown_ = function(e) {
- Listener: scratch-blocks/core/inject.js#L422 Blockly.bindEventWithChecks_(document, 'keydown', null, Blockly.onKeyDown_);

Here's the call stack when deleting a block with the Delete key (from Scratch Desktop 3.27.0):
Call Stack

@DNin01
Copy link
Member

DNin01 commented Oct 25, 2023

Oh, I can explain this.

When a key is pressed, one of the things Scratch checks for is if you're holding down Alt, Ctrl, or Command, then if you are, it checks if you're pressing C, X, or V. C copies, X cuts (copies and deletes), and V pastes.

So what we're actually doing is cutting the block. Typically, this is done with Ctrl+X but Alt+X also works because that was in the if condition for some reason.

Here's where that happens:

Blockly.onKeyDown_ = function(e) {
  // ...
  var deleteBlock = false;
  if (e.keyCode == 27) {
    // ...
  } else if (e.altKey || e.ctrlKey || e.metaKey) {
    // ...
    if (Blockly.selected && Blockly.selected.isDeletable() && Blockly.selected.isMovable()) {
      if (e.keyCode == 67) {
        // 'c' for copy.
        Blockly.hideChaff();
        Blockly.copy_(Blockly.selected);
      } else if (e.keyCode == 88 && !Blockly.selected.workspace.isFlyout) {
        // 'x' for cut, but not in a flyout.
        // Don't even copy the selected item in the flyout.
        Blockly.copy_(Blockly.selected);
        deleteBlock = true;
      }
    }
    if (e.keyCode == 86) {
      // 'v' for paste.
      // ...
    } else if (e.keyCode == 90) {
      // 'z' for undo 'Z' is for redo.
      Blockly.hideChaff();
      Blockly.mainWorkspace.undo(e.shiftKey);
    }
  }
  // Common code for delete and cut.
  // Don't delete in the flyout.
  if (deleteBlock && !Blockly.selected.workspace.isFlyout) {
    Blockly.Events.setGroup(true);
    Blockly.hideChaff();
    Blockly.selected.dispose(/* heal */ true, true);
    Blockly.Events.setGroup(false);
  }
};

@iqnite
Copy link
Contributor Author

iqnite commented Oct 25, 2023

Wait... I thought you could only copy/cut/paste with DevTools?

@DNin01
Copy link
Member

DNin01 commented Oct 25, 2023

Wait... I thought you could only copy/cut/paste with DevTools?

Yeah, Scratch has built-in copy, cut, delete, and paste functionality for the code area. You just need to focus the block you want to copy/cut/delete by clicking it (you can right-click or drag to avoid executing it) and then do the appropriate keyboard shortcut.

@WorldLanguages
Copy link
Member

I think the devtools addon just presses the shortcut on your behalf.

@iqnite
Copy link
Contributor Author

iqnite commented Oct 26, 2023

I've been overestimating its power for years...

I think the devtools addon just presses the shortcut on your behalf.

@DNin01
Copy link
Member

DNin01 commented Oct 27, 2023

From what I've found, it looks like the code area is the only place where Alt+X does anything. Everywhere else I've seen, either it's Ctrl+X or cutting isn't available, so to resolve the problem of pausing the project cutting a block, that should do it, without the need for a new addon. The only other notable problem I can think of is how arrow keys shift objects in costumes. Not sure if that can be fixed without a new addon. Anything else to consider?

@WorldLanguages
Copy link
Member

There are other related issues that talk about handling keyboard shortcuts/hotkeys and in the future trying to provide a way to customize all vanilla Scratch and addon shortcuts in the same place: #5145 #4946

@DNin01
Copy link
Member

DNin01 commented Nov 2, 2023

I'm not an expert on trapping Scratch internals, but would trapping Blockly let us deactivate this feature?

@WorldLanguages
Copy link
Member

Related feedback, I believe it's a Scratch bug.

pressing backspace in fullscreen deletes recently interacted block in the editor

@WorldLanguages
Copy link
Member

This is also somewhat related to this suggestion by griffpatch, regarding the middle-click-popup (insert blocks by name) addon:

image

After he suggested this, we tried to discuss how to determine if the user is trying to type into the stage or into the code area.

@WorldLanguages
Copy link
Member

I don't know if maybe clicking on the stage (to focus it) first could prevent this from happening? Edit: it does not!

I think that might be intentional, as some users might not notice they need to click the stage...

We should probably try to find a way to make this somewhat intuitive, plus making sure SA users enabling this new addon read the addon description, and it would also have the side-effect of making griffpatch's idea possible to implement.

@DNin01
Copy link
Member

DNin01 commented Nov 5, 2023

So, the Blockly onKeyDown_ function can be accessed like so:

export default async function ({ addon, console }) {
  const Blockly = await addon.tab.traps.getBlockly();
  console.log(Blockly.onKeyDown_);
}

But I haven't figured out how to deactivate it yet.

@WorldLanguages
Copy link
Member

But I haven't figured out how to deactivate it yet.

Multiple addons do this, basically you replace it with another function that only calls the original function if addon.self.disabled

@DNin01
Copy link
Member

DNin01 commented Jan 28, 2024

What if you could individually toggle keyboard shortcuts? For example, I could have backspace to delete enabled but turn off all of the Ctrl-key shortcuts which I never use in the Scratch code editor.

@WorldLanguages
Copy link
Member

Off-topic: Sometimes I use Backspace or Delete to delete blocks, but I did not know that x does too! (Apparently, it's actually Alt+x.)

Ugh, and we thought Alt+X for pausing was a unique combination...

+1 from feedback
I think it needs its own dedicated issue now

@WorldLanguages
Copy link
Member

I created issue #7225

@mybearworld
Copy link
Contributor

Here, Blockly.onKeyDown_ is added as an event listener directly. So I don't think changing Blockly.onKeyDown_ afterwards will do anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new addon Related to new addons to this extension. `scope: addons` should still be added. scope: addon Related to one or multiple addons type: enhancement New feature for the project
Projects
None yet
Development

No branches or pull requests

4 participants