Is it possible to wait for either button A or button B? - micropython

This is code that waits for an event coming from button A.
control.waitForEvent(Button.A, EventBusValue.MICROBIT_EVT_ANY)
I want to wait for either button A OR button B to be pressed.
Is this possible?
EDIT
I know that in Scratch this is possible with something like button.any, also the above code is written in microjavascript, but it is written similarly in micropython, so maybe someone from this field can also help. Thank you.

Have a look at the python code generated using the 'on event from' block in https://makecode.microbit.org. This block can be found under the Advanced tab. The block can be set to wait for either button A or B to be pressed. The micropython code generated in this case is:
control.on_event(EventBusSource.MICROBIT_ID_BUTTON_AB,
EventBusValue.MICROBIT_EVT_ANY,
on_microbit_id_button_ab_evt)
The equivalent JavaScript code can also be viewed in the editor.

The best way I could do it was with common function for A nad B, to be called on events of button pressed, like:
input.onButtonPressed(Button.A, function () {
qAndA(true)
})
input.onButtonPressed(Button.B, function () {
qAndA(false)
})
The true and false are not the best implementations but to know which button was pressed, I needed to pass the true for A and false for B.
This was required in my case, because I was writing test and later in this function I would compare the button pressed and the actual correct answer. The function (without my full implementation) goes something like this:
function qAndA(aOrB: boolean) {
if (text_list.length == 0) {
basic.showNumber(count)
basic.pause(2000)
basic.showLeds(`
# # # # #
. . # . .
. . # . .
. . # . .
. . # . .
`)
}
if (text_list[0] == Q1 && aOrB == true) {
...
}
}

In scratch, placing a [Wait for <Key Pressed (a) ||or|| Key Pressed (b)>], will do the trick, you can just slot a key pressed (a) and a key pressed (b) into the same or statement, and place the new blocks into a "Wait until" block, place this where you want the code to stop and continue on the keypress update and you're golden.

Related

Executing a function when key combination is pressed

I'm adding some functionality to a menubar app. I want to execute a few lines of code that copies some text to the clipboard when a combination of keys are pressed (e.g. cmd + alt + L). This should work globally, i.e any time these keys are pressed.
Not sure how to go about doing this, I tried overriding the keyDown method but it gives an error in AppDelegate.swift saying that there's no method to override.
First step you need to add a global monitor.
NSEvent.addGlobalMonitorForEvents(matching: .keyDown, handler: {
self.keyDown(with: $0)
})
But it can be also your func.
Second step is to handle these three keys
Read flags from NSApp.currentEvent?.modifierFlags and check if they contain .option and .command flags
Example
guard let flags = NSApp.currentEvent?.modifierFlags else {
return
}
let optionKeyIsPressed = flags.contains(.option)
At last key you can read from NSEvent property keyCode.
The keyCode of later "L" you can read from kVK_ANSI_L
Hope it's all that you need to solve your problem, good luck.

In MS Access VBA determine the next control to have focus

Understanding the order of operations, is there a way for an OnExit or OnLostFocus script to know what control was clicked to take that focus?
I have a form that has a list box from which you have to select an item for the the rest of the data entered into the form to be saved, so that control has the focus when you open the form. If you click on any other control without selecting an item from the list, an OnExit gives you a warning that you haven't made a selection.
The problem that creates is, should you open the form and then decide you don't want it, you get that message when you close the form (either with the close button on the form or the built-in closer).
I would like to be able to tell the OnExit that either the close button has been clicked or the form itself is closing, so don't show the no entries message.
Any thoughts would be appreciated.
Not that I know of.
If your form is rather simple(not to many controls.) Move the warning out of the onExit to a sub. Call it from the other controls when they get focus. But not from the Close button.
Dim bDidUserSelectSomething As Boolean
'In the list box
Private sub ListBox1_Click()
'Do some code here
'Set the flag that they did something
bDidUserSelectSomething = True
End sub
'When you enter the other controls, check if they have done what they should have.
Private Sub TextBox1_Enter()
If bDidUserSelectSomething = False Then
'Warning
End If
End Sub
Consider altering your warning check.
Instead of an OnExit() Event of the listbox, add a validation in all other pertinent controls' BeforeUpdate() events. Usually this event is where validation checks are made.
First, build a general function behind the form that calls your warning:
Public Function WarningMessage() As Boolean
If IsNull(Me.Listbox) Then
Msgbox "Please first select an option in listbox.", vbExclamation, _
"MISSING LISTBOX SELECTION"
WarningMessage = True
Else
WarningMessage = False
End If
End Function
Then call the function to each control:
Private Sub cntr1_BeforeUpdate(Cancel As Integer)
Cancel = WarningMessage
End Sub
Private Sub cntr2_BeforeUpdate(Cancel As Integer)
Cancel = WarningMessage
End Sub
...
As you can see from above, if the listbox is null then the message appears and returns True which is passed back to the control's BeforeUpdate() subroutine to cancel the update.
Now, if there are too many controls to make this doable. Consider hiding all relevant controls in Form's OnCurrent() event except the listbox. When listbox is updated, render the other controls visible. Many softwares and web applications run dynamically like this to prevent users from missing important fields.
Private Sub Form_Current()
If IsNull(Me.listbox) Then
Me.ctnrl1.Visible = False
Me.ctnrl2.Visible = False
Me.ctnrl3.Visible = False
...
Else
Me.ctnrl1.Visible = True
Me.ctnrl2.Visible = True
Me.ctnrl3.Visible = True
...
End if
End Sub
Private Sub listbox_AfterUpdate()
Me.ctnrl1.Visible = True
Me.ctnrl2.Visible = True
Me.ctnrl3.Visible = True
...
End Sub

perl tk listbox, detect when focus is lost via mouse

I have two listboxes in a small perl/tk script. When I click on one, the other "loses focus" and the clicked one "gains" it. I put that in quotes because unfortunately these events do not trigger "<FocusIn>" or "<FocusOut>". Using the keyboard, ie, the tab key, does trigger these. I have also tried <Enter>/<Leave> and <B1-Enter>/<B1-Leave> as well as <<ListboxSelect>> but none of these achieve what I need. I listed the available events to be triggered, but most are keyboard related.
What I need is to disable a Button when the second ListBox loses that focus (ie, when the first ListBox is clicked on), and enable it when it gains it via the mouse. So how do I do this?
Ok, I found an acceptable solution for this:
my $tmp = ref $my_listbox;
$my_listbox->bind($tmp, '<<ListboxSelect>>', sub { &listbox_bind; } );
sub listbox_bind
{
my ($self) = #_;
if ($self == $my_listbox)
{ $my_button->configure( -state => 'normal' ); }
else
{ $my_button->configure( -state => 'disabled' ); }
}
hope that helps someone out there.

Jstree dblclick binding problem [duplicate]

I try to use good lib jstree but i have some strange problem with dblclick binding.
Here is my code
$("#basic_html").jstree({
themes: {
url: "http://mywork/shinframework/shinfw/themes/redmond/css/jstree/default/style.css"
},
"plugins" : ["themes","html_data","ui","crrm","hotkeys", "core"],
});
$("#basic_html").bind("dblclick.jstree", function (e, data) {
alert(e);
alert(data);
});
When this code runs and i make dblclick for some node i can see 2 alerts. The first is object -right, the second is undefined - BUT i want receive data information.
Please, if some specialist solve this problem give me right way for correct use dblclick and receive "data" information about node who is i clicked.
Thanks
I recommend this approach . . .
$("#basic_html li").live("dblclick", function (data) {
//this object is jsTree node that was double clicked
...
});
First, you usually only need to know if the li was clicked so monitoring the event on the li will give you everything you need. Secondly, use live or delegate for the event binding so you can manipulate the tree without breaking the event.
Once you have the node that was double clicked (the this object) you can then use the built-in functions like this . . .
if (!jsAll.is_selected(this)) { return false; } //cancel operation if dbl-clicked node not selected
Where . . .
jsAll = $.jstree._reference("basic_html")
$("#basic_html").bind("dblclick.jstree", function (event) {
var node = $(event.target).closest("li");//that was the node you double click
});
that's the code you want.

Jstree : dblclick binding parameter data is undefined

I try to use good lib jstree but i have some strange problem with dblclick binding.
Here is my code
$("#basic_html").jstree({
themes: {
url: "http://mywork/shinframework/shinfw/themes/redmond/css/jstree/default/style.css"
},
"plugins" : ["themes","html_data","ui","crrm","hotkeys", "core"],
});
$("#basic_html").bind("dblclick.jstree", function (e, data) {
alert(e);
alert(data);
});
When this code runs and i make dblclick for some node i can see 2 alerts. The first is object -right, the second is undefined - BUT i want receive data information.
Please, if some specialist solve this problem give me right way for correct use dblclick and receive "data" information about node who is i clicked.
Thanks
I recommend this approach . . .
$("#basic_html li").live("dblclick", function (data) {
//this object is jsTree node that was double clicked
...
});
First, you usually only need to know if the li was clicked so monitoring the event on the li will give you everything you need. Secondly, use live or delegate for the event binding so you can manipulate the tree without breaking the event.
Once you have the node that was double clicked (the this object) you can then use the built-in functions like this . . .
if (!jsAll.is_selected(this)) { return false; } //cancel operation if dbl-clicked node not selected
Where . . .
jsAll = $.jstree._reference("basic_html")
$("#basic_html").bind("dblclick.jstree", function (event) {
var node = $(event.target).closest("li");//that was the node you double click
});
that's the code you want.