OnKeyPress Subroutine Executed Twice
Moderators: JRL, Dorian (MJT support)
OnKeyPress Subroutine Executed Twice
Dialog>Dialog1
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSComboBox2,OnKeyPress,DoKeyPress
Show>Dialog1,r
Debugger tests show AddDialogHandler always runs twice when an item is selected from MSComboBox2 by pressing ENTER. But why?
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSComboBox2,OnKeyPress,DoKeyPress
Show>Dialog1,r
Debugger tests show AddDialogHandler always runs twice when an item is selected from MSComboBox2 by pressing ENTER. But why?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Not in my case. I am only seeing one execution of the OnKeyPress subroutine when pressing Enter.
The following code increments a counter which appears in the dialog's title bar. If you press Enter while on the combo box you will see the counter is incremented by one only:
The following code increments a counter which appears in the dialog's title bar. If you press Enter while on the combo box you will see the counter is incremented by one only:
Code: Select all
Let>x=0
AddDialogHandler>Dialog1,MSComboBox1,OnKeyPress,DoKeyPress
Show>Dialog1,r
SRT>DoKeyPress
Let>x=x+1
SetDialogProperty>Dialog1,,Caption,x
END>DoKeyPress
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Marcus,
Thanks for taking time to explore the OnKeyPress subroutine issue.
In my case, it still repeats twice.
In my case, it occurs in the 2nd combobox.
The contents in the 2nd combox are constructed on the fly depending on the combobox1 selection, ie, DoChgList as shown below.
I have to use variable DoKeyPressRun to stop SRT>DoKeyPress from running twice. Here the DoKeyPress simulates pressing &Ok button.
The debugger shows, after completing the DoKeyPress, the execution pointer(_LINE_NUM) is returned back to **BREAKPOINT** again.
Let>DoKeyPressRun=1
Dialog>Dialog1
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSComboBox1,OnSelect,DoChgList
AddDialogHandler>Dialog1,MSComboBox2,OnKeyPress,DoKeyPress
SRT>DoKeyPress
**BREAKPOINT**
IF>DoKeyPressRun=1
If>{(%DoKeyPress_KEY%=13) or (%DoKeyPress_KEY%=32)}
Let>ModalResult=1
Press ALT
Send>o
Release ALT
Add>DoKeyPressRun,1
Endif
Endif
End>DoKeyPress
Thanks for taking time to explore the OnKeyPress subroutine issue.
In my case, it still repeats twice.
In my case, it occurs in the 2nd combobox.
The contents in the 2nd combox are constructed on the fly depending on the combobox1 selection, ie, DoChgList as shown below.
I have to use variable DoKeyPressRun to stop SRT>DoKeyPress from running twice. Here the DoKeyPress simulates pressing &Ok button.
The debugger shows, after completing the DoKeyPress, the execution pointer(_LINE_NUM) is returned back to **BREAKPOINT** again.
Let>DoKeyPressRun=1
Dialog>Dialog1
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSComboBox1,OnSelect,DoChgList
AddDialogHandler>Dialog1,MSComboBox2,OnKeyPress,DoKeyPress
SRT>DoKeyPress
**BREAKPOINT**
IF>DoKeyPressRun=1
If>{(%DoKeyPress_KEY%=13) or (%DoKeyPress_KEY%=32)}
Let>ModalResult=1
Press ALT
Send>o
Release ALT
Add>DoKeyPressRun,1
Endif
Endif
End>DoKeyPress
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Can you send me your dialog code.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
You're sending CTRL-O. Where do you want to send that? If you are not using SetFocus then it will land on the combo box. In which case then of course it will trigger the OnKeyPress.
If you don't want it to land on the combo box then insert a SetFocus to make it go someplace else intended and then the OnKeyPress won't fire.
If you DO want it to land on the combo box then set some kind of flag in your OnKeyPress subroutine so that the code cannot fire until you're done.
If you don't want it to land on the combo box then insert a SetFocus to make it go someplace else intended and then the OnKeyPress won't fire.
If you DO want it to land on the combo box then set some kind of flag in your OnKeyPress subroutine so that the code cannot fire until you're done.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Marcus,
Your idea is very helpful.
2 more questions:
Q1: I add SetDialogObjectFocus so that the focus shifts from combox2 to the Ok button. Still DoKeyPress is fired 2nd time.
SRT>DoKeyPress
**BREAKPOINT**
SetDialogObjectFocus>Dialog1,MSButton1
IF>DoKeyPressRun=1
If>{(%DoKeyPress_KEY%=13) or (%DoKeyPress_KEY%=32)}
Let>ModalResult=1
Press ALT
Send>o
Release ALT
Add>DoKeyPressRun,1
Endif
Endif
End>DoKeyPress
Q2
We have AddDialogHandler>Dialog1,MSComboBox2,OnKeyPress,DoKeyPress.
What if we wish to disable DoKeyPress inside the DoKeyPress subroutine?
That's in Macro Scheduler, we can enable to OnKeyPress event subroutine. It doesn't seem unable to disable it during the runtime.
Your idea is very helpful.
2 more questions:
Q1: I add SetDialogObjectFocus so that the focus shifts from combox2 to the Ok button. Still DoKeyPress is fired 2nd time.
SRT>DoKeyPress
**BREAKPOINT**
SetDialogObjectFocus>Dialog1,MSButton1
IF>DoKeyPressRun=1
If>{(%DoKeyPress_KEY%=13) or (%DoKeyPress_KEY%=32)}
Let>ModalResult=1
Press ALT
Send>o
Release ALT
Add>DoKeyPressRun,1
Endif
Endif
End>DoKeyPress
Q2
We have AddDialogHandler>Dialog1,MSComboBox2,OnKeyPress,DoKeyPress.
What if we wish to disable DoKeyPress inside the DoKeyPress subroutine?
That's in Macro Scheduler, we can enable to OnKeyPress event subroutine. It doesn't seem unable to disable it during the runtime.
It contains typos, alas.armsys wrote: After submitting my last post, I realize my code does press keys in the DoKeyPress:
Press CTRL
Send>o
Release CTRL
It should read:
Press ALT
Send>o
Release ALT
Its purpose is to simulate pressing the Ok button.
The whole picture is: when selecting an item in combobox2 by pressing ENTER, it's assumed the user accepts the selections from combox1 and combobox 2. Therefore, the Ok button is presssed programmatically.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Why do you want to send alt-o to an ok button you created and control? Seems a bit odd. Why not just do whatever the OK button triggers. There's no need to send keystrokes to your own dialog!
You can't disable a handler at this time, but you could use a flag as already suggested. Check the value of the flag at the start of the routine and only proceed if it is set/unset.
You can't disable a handler at this time, but you could use a flag as already suggested. Check the value of the flag at the start of the routine and only proceed if it is set/unset.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?