Some reason when I use a key event on a second window, the event is fired twice. Issue is there for image buttons and normal buttons. Have also tried the new update to the library but no joy.
Here’s repeatable code:
import g4p_controls.*;
import java.awt.Font;
GWindow qBox = null;
GLabel lblQBox;
GImageButton imgBtnOKQBox;
GImageButton imgBtnCancelQBox;
int buttonWidth = 80;
int buttonHeight = 30;
int buttonXOffset = 100;
int buttonYOffset = 50;
public void setup(){
size(300, 300, JAVA2D);
createGUI();
};
public void draw(){
background(200);
}
public void qBoxDraw(PApplet app, GWinData data){
qBox.background(127);
}
void showQBox(String strTitle, String strMessage) {
int windowWidth = 300;
int windowHeight = 160;
if (qBox == null) {
qBox = GWindow.getWindow(this, strTitle, width/2 + 120, height/2 + 65, 300, 160, JAVA2D);
qBox.setActionOnClose(G4P.HIDE_WINDOW);//HIDE_WINDOW
qBox.addDrawHandler(this, "qBoxDraw");
qBox.addKeyHandler(this, "qBoxKey");
btnOK = new GButton(qBox, 50, 100, 80, 30);
btnOK.setText("OK");
btnOK.addEventHandler(this, "btnOK_click");
btnCancel = new GButton(qBox, 175, 100, 80, 30);
btnCancel.setText("Cancel");
btnCancel.addEventHandler(this, "btnCancel_click");
}//if (qBox == null) {
else {
//imgBtnOKQBox.addEventHandler(this, "btnOK_click");
//qBox.addKeyHandler(this, "qBoxKey");
//qBox.setTitle(strTitle);
//qBox.setVisible(true);
}
}
public void qBoxKey(PApplet app, GWinData data, KeyEvent event) {
if (event.getKey() == RETURN || event.getKey() == ENTER) {
onYes();
}
}
public void btnOK_click(GButton source, GEvent event) {
onYes();
}
public void onYes(){
qBox.setVisible(false);
println();
println("RUNNING KEY EVENT!");
}
public void btnCancel_click(GButton source, GEvent event) {
qBox.setVisible(false);
}
public void btnTest_click(GButton source, GEvent event) { //_CODE_:button1:499313:
println("button1 - GButton >> GEvent." + event + " @ " + millis());
showQBox("Title","Message");
} //_CODE_:button1:499313:
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setMouseOverEnabled(false);
surface.setTitle("Sketch Window");
btnTest = new GButton(this, 102, 130, 80, 30);
btnTest.setText("Test");
btnTest.addEventHandler(this, "btnTest_click");
}
GButton btnTest;
GButton btnOK;
GButton btnCancel;
If you click the Test button, then use the Enter key to select OK, the event fires twice. See serial monitor for output.
I’m SURE I’m missing something, so apologies if this is something stupid. This is causing backups to be run twice, so if there’s any way to get rid of this double firing I’d be grateful.
Oh, I do have an awkward workaround, but it’s really bad coding practice the way I have the workaround.
Thanks for any insights.
Mike