Textbox lostfocus event doesn't fire when using a default button - textbox

Today I stumbled upon a problem with a LostFocus event from a TextBox that didn't fire. Most clients didn't have any problems but a small portion of them reported unexpected behavior. After some research I found that the clients who didn't had the problem clicked on the "Ok" button with the mouse while the other clients pressed Enter on their keyboard. The "Ok" button was the default button on the Form so pressing Enter should work just fine. The problem is that pressing Enter doesn't fire a LostFocus event on the TextBox with focus.
After some Googling it was pretty clear that this is the expected behavior of a default button. The focus never loses the TextBox and the code behind the CommandButton Click event is being run without it being clicked.
How to get the LostFocus event to fire when using a default button?

A simple hack that worked for me is to set the focus to the "Ok" button whenever the Click event is being fired. That way the current control automatically runs its LostFocus event. Don't forget to put an extra DoEvents after setting the focus. Otherwise the LostFocus event fire after your other code has been executed.
Private Sub cmdOk_Click()
cmdOK.SetFocus
DoEvents
'Run your other code
End Sub

Related

Delphi - ComboBox - Click event handler running when key is pressed

I'm using Delphi where I have a ComboBox on a form - in the Click event handler I'm calling ShowMessage('click') while in the KeyPress event handler ShowMessage('KeyPress'). When the ComboBox has the focus, and I press a 'normal' key like 'd', first the KeyPress event handler is running then follows the Click event handler.
Interesting: when the combobox's style property is set to csDropDownList or csDropDown, the Click event handler runs even twice, when it's set to csSimple, it runs 'only' once.
So I can't easily have a combobox where i (in the click event handler) set the focus to the next component after the user selected an item by clicking, since when the user is pressing keys the click event handler will run and set the focus to the next component. so what i planned for usability purposes would drive him crazy.
I wished the Delphi developers would be forced to use such a program for a whole week, 8 hours a day - I'd set the forms caption to sth like 'Never ever trigger the click event handler when the mouse wasn't really clicked!' - Though the problem is well known FOR MANY YEARS now, and Delphi costs REALLY much, the problems still exists.
Is there anyone with a good solution?

Edge browser: Events stop working, when any browser control is clicked

My extension for the Edge browser adds some mouse and keyboard events to the current page. The extension is launched from the browser action element.
The problem is that when the browser action element is clicked, the current window loses its focus, and the events stop firing, until the page is focused back by mouse clicking any page area.
I setup my event handlers like that:
wrapper.addEventListener('keydown', onKeyDown, true);
wrapper.addEventListener('mousemove', selectElementMouseMove, true);
I tried window.focus(), document.body.focus(), chrome.tabs.update, chrome.window.update - they do not help.
The focus is also lost, when I click any element of the browser outside of the web page area.
Is there any way to prevent this behaviour?

jQueryUI modal dialog doesn't block RETURN keyup event (or ESCAPE)

I'm using a jQueryUI modal dialog widget to simulate an "alert" box. It all works as expected, with keyboard input being blocked while the dialog is open, except that if I close the dialog with the RETURN or ESCAPE key then I get a stray "keyup" event hitting my application. Why is this happening, when a modal dialog is supposed to block all input?
Here's a JSfiddle demonstrating the problem - https://jsfiddle.net/kmbro/x7w6hLtg/ - and here's some code to make StackOverflow happy about the JSfiddle link :-)
$("<div/>").dialog({ modal: true, ... });
While dialog() appears to be the culprit for allowing the ESCAPE "keyup" event to leak, I don't think it's responsible for RETURN. This seems to centre on how button "click" events are generated by the RETURN key, because they fire on "keydown" whereas using the spacebar to press the button triggers the "click" on "keyup", so the dialog has closed before the "keyup" event occurs. You can see this by pressing the Click Me! button by tabbing onto it and pressing RETURN - the dialog opens on "keydown". If you press the button using SPACE instead then the dialog doesn't open until the "keyup" when you release the key.
The question is, what's so special about RETURN that is triggers a "click" event on "keydown" and not "keyup"?
It seems that the RETURN key triggering "keydown" - "click" - "keyup" is simply the way browsers work - see https://bugs.jqueryui.com/ticket/15194 - so I'll just have to learn to live with it.

Jquery UI date picker kicks users off page if they hit backspace in IE

I'm using jqueryUI for a datepicker to pick birthdays. Turns out in IE if the person tries to type in the box instead of using the picker, and hits the backspace, they get kicked off the form and to a previous page. Can anyone explain or propose a fix? Also not sure why the red asterisk is showing up inside the box when it shows outside in firefox and when all the others display correctly.
Looking at the eighth field:
http://www.craftonhills.edu/Degrees_and_Certs/Divs_and_Depts/Career_Education_and_Human_Development/Public_Safety_and_Services/Fire_Technology/Firefighter_Academy/Firefighter_Academy_Info/Application
Backspace key in the browser defaults to triggering the Back button. You can try to intercept the keystroke (See here: Intercepting all key presses with jQuery before they are processed) to stop it from firing, but a better solution might be to add a "Clear" button to the jQuery UI Element to remove the field so the back button still works as intended.
Try the following:
$(document)
.on('keypress keydown keyup', '.datepicker', function(e) {
if ( e.keyCode == 8 ) e.stopPropagation();
});
Replace .datepicker with whatever selector you are using.
This should allow the backspace to work as expected in the text block but still prevent the backspace (from bubbling up the DOM) from triggering the browser back action.

blur event not firing on iOS Mobile Safari in Sencha Touch

I'm using iOS 5.0.1, and Sencha Touch 2-rc1. I have a search input field where the focus event is getting triggered, as well as the submit event when I press 'Search' on the on-screen keyboard. The blur event doesn't get triggered when I expect it to, which would be when the 'Done' key is pressed, or the viewable area is tapped.
Note that the blur event IS getting triggered on my laptop in Chrome.
Not every element is focusable. At least <div> is not.
onblur is not firing because when a user taps on a div element, the focus doesn't go to the <div>.
Based on this post:
http://snook.ca/archives/accessibility_and_usability/elements_focusable_with_tabindex
tabindex on the correct div element can make a div focusable.
This is likely to be related to the event not "bubbling" up through the DOM. Or perhaps the code you've used includes an event.preventDefault(), but that would have killed more than just blur. I've also had this issue with clicking away from items which appear via javascript.
http://www.quirksmode.org/dom/events/blurfocus.html

Resources