Intercept keyboard paste event on certain condition

47 views
Skip to first unread message

Aparna Gk

unread,
Sep 8, 2022, 3:18:40 AM9/8/22
to Blockly
Hi Team,

How to override the keyboard paste (ctr+c) behavior in the blockly workspace based on certain conditions.

Is there an 'easy' way to intercept a paste event ?


Thanks in Advance,
Aparna GK

Aparna Gk

unread,
Sep 8, 2022, 9:19:10 AM9/8/22
to Blockly
Hi Team,

I identified below is the function that I can use to override for paste event

Capture.JPG
this function is not being triggered on paste of block in the workspace, could some one help me in identifying what could be the issue ?
 
Thanks in Advance
Aparna GK

Christopher Allen

unread,
Sep 12, 2022, 12:16:40 PM9/12/22
to blo...@googlegroups.com
Hi Aparna,

How to override the keyboard paste (ctr+c) behavior in the blockly workspace based on certain conditions.

Is there an 'easy' way to intercept a paste event ?

The most straightforward way to do this is to override the existing shortcut with your own.  Something like:

const PASTE = Blockly.ShortcutItems.names.PASTE;
const oldPasteShortcut = Blockly.ShortcutRegistry.registry.getRegistry()[PASTE];
const newPasteShortcut = {
  ...oldPasteShortcut,  // Use original .name, .preconditionFn and .keyCodes.
  callback(workspace) {
    // Your implementation goes here, which might do the two things
    // done by the original implementation:
    workspace.hideChaff();
    workspace.undo(false);
    // And should end with:
    return true;
}
Blockly.ShortcutRegistry.registry.register(newPasteShortcut, /*allowOverrides:*/ true);

You may find it useful to compare against the original registerPaste implementation, and/or against the cross-tab-copy-paste plugin which first unregisters the old shorcut and then registers a completely new one (though N.B. that this code uses a fairly old style, where the keys are mapped separately after adding the shortcut; it would be better to include them in the KeyboardShortcut object itself as is done by the more modern code in registerPaste).

I identified below is the function that I can use to override for paste event

Capture.JPG
this function is not being triggered on paste of block in the workspace, could some one help me in identifying what could be the issue ?

Trying to monkey-patch Blockly (by e.g. assigning to Blockly.paste) is no longer supported.  Indeed, in future versions of Blockly this assignment will throw, because ES6 Module objects are immutable; it only "works" at all for now because of transpilation to ES5.


I hope this provides a useful place to start.


Christopher

Reply all
Reply to author
Forward
0 new messages