How to know input field focus or not

254 views
Skip to first unread message

yixin

unread,
Jun 2, 2021, 11:50:40 PM6/2/21
to Blockly
Hi all,
In my app, I have two workspace, one for display, one just hidden, and when user fire event will use hidden workspace to replace display workspace. The problem is, once user type input field like 'math_number' , 'Change' Event will fire and replace the display workspace, and input field lost focus because all block is new block.
So my idea is when user focus on input field, set a flag not replace display workspace until input field blur. Is there any solution?

Beka Westberg

unread,
Jun 3, 2021, 4:32:52 PM6/3/21
to blo...@googlegroups.com
Hello,

In general Blockly does not fire blur events for fields :/ However there are two ideas I have that might work for you:
1) Filter events based on their type. IE don't switch the workspace on change events. But I'm not sure if this will work for you because I'm not sure why you're switching the workspaces hehe.
2) For text input fields specifically, you can assign a function to the onFinishEditing_ property of the field. This function will then be called whenever the text input field loses focus (ie when the user is finished editing the field). For example, you can do something like this:

```
Blockly.Blocks['my_block'] = {
  init: function() {
    var field = new Blockly.TextInput();
    field.onFinishEditing_ = this.myTriggerWorkspaceSwitchFn;
  },
  myTriggerWorkspaceSwitchFn = function() { ... }
}
```

I hope that helps! If you have any further questions please reply!
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/9c24718a-3b47-43d0-8ce3-8d765e1a68d8n%40googlegroups.com.

yixin

unread,
Jun 3, 2021, 10:51:34 PM6/3/21
to Blockly
Hi Beka!
I want to know is there any way to modify  'field' e.g. dropdown or text to add `onFinishEditing_`
If it can I don't need to modify every block that has 'field'

Thanks!

Beka Westberg

unread,
Jun 4, 2021, 4:16:55 PM6/4/21
to blo...@googlegroups.com
onFinishEditing_ is only available for text input fields (and the subtypes number and angle) at the moment. Other fields like dropdowns and checkboxes are close immediately after their value is edited, so there's no need to have the extra callback =)

I think you can add a callback to all of the text input fields by monkey-patching the prototype:
```
Blockly.FieldTextInput.prototype.onFinishEditing_ = myGeneralFunction;
```

But I haven't tested it, so I'm not sure. And there's no guarantee that monkey patches will continue working in the future.

I hope that helps! Best of luck on your project :D
--Beka

Reply all
Reply to author
Forward
0 new messages