How to stop Gxt TextBox KeyPress event in FF as it is working on IE - keypress

In IE when the 'KeyPress' Event is not calling on pressing of navigation keys, but in FF it is firing the 'KeyPress' Event, how to stop this.
I am using Gxt 'TextField' with KeyPress event as follows
firstNameField.addListener(Events.KeyPress, getKeyPressListener());
private Listener<FieldEvent> getKeyPressListener(){
return new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
char key = getChar(be.getEvent());
if (!((key+"").matches("^[A-Za-z \\'\\-\\&]+$"))) {
be.stopEvent();
}
}
// needed due to GWT 2.1 changes
private native char getChar(NativeEvent e) /*-{
return e.which || e.charCode || e.keyCode || 0;
}-*/;
};
}
I have configured some regular expression which allows user to enter only 'A-Za-z','-' and '&', other than these values it stops the user from entering.
This code is working fine in IE, as it is not firing 'KeyPress' event on navigation key press, but when it comes to FF 'KeyPress' event is firing on every key hit. So to make 'FF' to work as similar to 'IE'

Related

Computed Knockout JS function seems not unbind in firefox for keypress event

I'm using the following function with knockout
self.navigateToSection = ko.pureComputed({
read: function() {
return this;
},
write: function(data, event) {
if (event.type == "keydown") {
if ((event.keyCode != '13')) {
return;
}
}
... (more actions)
}//write
});
this is the HTML binding
data-bind="event: {keypress: navigateToSection}">
The function should help to navigate between different sections so the user must to be able to press the tab key and jump to a different link. it works well for most of the browser except for firefox in Windows 10 it's working fine in Mac for Firefox
I found that keypress event is no longer fired for non-printable keys, except for the Enter key, and the Shift + Enter and Ctrl + Enter key combinations for Firefox. So when I press tab it seems stock there instead to jump to the next HTML element with a tabindex attribute if I blur the element it allows to jump even in FF but just until the second keypress.
I try to unbind the element but it does not work well.
Any ideas?
The keypress event has been deprecated. You should use keydown instead.

execCommand doesn't fire when called from keydown event in Windows Store apps

I'm writing a windows store app (HTML) that includes some simple rich-text editing. I can apply bold to the currently selected using a button which fires document.execCommand("bold",false,null);
However when I bind this to a keydown event like CTRL+B, nothing happens. Here's my keydown code.
document.addEventListener("keydown", catchShortCuts, false);
function catchShortCuts(e) {
if (e.ctrlKey) {
if (e.keyCode == 66) // b
document.execCommand('bold', true, null);
}
}
}
I know my keydown code works fine because if I replace the document.execCommand with another line of code it fires just fine when I press CTRL+B. It seems like execCommand has a problem with the keydown event?
Turns out that it works fine if you use keypress instead of keydown. Just in case anyone else has the same issue, that's a workaround. Still not sure why onkeydown doesn't work though.
Working code:
document.addEventListener("keypress", catchShortCuts, false);
function catchShortCuts(e) {
if (e.ctrlKey) {
if (e.keyCode == 66) // b
document.execCommand('bold', true, null);
}
}
}

ChangeValueHandler not fired when clicking on the back button In GWT

I have a form and I want to display a confirmation dialogBox when the user presses the back button. Let say that I have one Texbonx that listens to a ChangeValueHandler
addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
setChanged(true);
}
});
This is the short scenario
1) I enter text in the TextBox
2) I hit the back button
3) The ValueChangeEvent is not called
I tried the fire the BlurEvent programmatically on the TextBox with textBox.fireEvent(new BlurEvent() { }); but still no result.
Any suggestions ?
When the page is closed (by pressing the back button, or closing the page) no events will be fired by the controls. What will fire first is window.onbeforeunload to give you a chance to warn the user about data loss and offer to stay on the page. If the user chooses to stay on the page, then all the events that were supposed to be fired, will fire (so your change event will fire).
You can attach a handler to the native onbeforeunload event by using Window.addClosingHandler.
Window.addWindowClosingHandler(new ClosingHandler() {
#Override
public void onWindowClosing( ClosingEvent event )
{
event.setMessage("If you leave the page now all data will be lost.");
}
});
It's worth noting that the ClosingEvent and it's underlying onbeforeunload event, cannot, under any circumstances, be cancelled programmatically. The only way to prevent the user from leaving the page is if the user itself chooses "Stay On This Page" in the popup that results from the code above.
What I did is to set the focus of the TextBox to False and then check if it's changed, that forces the TextBox to unfocus when hiting the back button.
This is the code that check if a form is changed
public boolean isChanged(){
if(formPanel == null) return false;
for (int i = 0; i < formPanel.getWidgetCount(); i++) {
if(formPanel.getWidget(i) instanceof BaseWidget){
BaseWidget w= (BaseWidget) formPanel.getWidget(i);
w.setFocus(false);
if(w.isChanged()){
return true;
}
}
}
return false;
}

Gwt : How to not to fire a click event after focus?

I've created some elements of my GUI with GWT framework. I've just one button with an simple onCLick method. And when the button gets the focus (setfocus(true)) the triogger fires a click event automatically. But I just want the button holds the focus without fire any events .
How to make it in a simple way ?
my code :
public void onModuleLoad(){
..............
textBoxTx = new TextBox();
textBoxTx.addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
switch(event.getNativeKeyCode()){
case KeyCodes.KEY_ENTER: addTx();
}
}
});
....
protected void addTx()
final String tx = textBoxTx.getText().toUpperCase().trim();
textBoxTx.setFocus(true);
if (!tx.matches("^[0-9\\.]{1,10}$")) {
Window.alert("'" + tx + "' n'est pas valide .");
textBoxTx.selectAll();
return;
}
textBoxTx.setText("");
param_Tx=Double.parseDouble(tx);
if (param_An==0)
rateFlexTable.setText(1, 2, tx);
else
{
for (int i=1;i<=param_An;i++)
rateFlexTable.setText(i, 2,tx);
rateFlexTable.setText(param_An, 4,"");
}
**// fire the click event of my button when I give the focus**
**btnCalcul.setFocus(true)**
}
If you have a standard com.google.gwt.user.client.ui.Button, calling setFocus(true) on it will not activate the ClickHandlers for that button. It may be good if you could share some code because what you describe should not happen unless you are explicitly calling Button.click() or the user is actually click on the button.

How can I prevent Firefox from opening the gridview header sort postback link in a new tab on Ctrl Click

I am trying to make my gridview control in ASP.Net do a multi sort based on if the user pressed Ctrl key when trying to sort by clicking on a column name. The problem is that when I am using Firefox, if I click on a column name with Ctrl key pressed, the browser tries to open "javascript:__doPostBack('ctl00$ContentPla..." link in a new tab. IE and Chrome both don't do this unless the link is a real link.
Is there a way I can prevent Firefox from opening the link in a new tab and still cause the page to postback normally?
Thanks.
You need to capture the event of the Ctrl key being pushed down, using document.onKeyDown.
In your event handler, check if 'Ctrl' (key code 17) was pressed, as follows:
function document_keyDown(e) {
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if (KeyID == 17) {
ctrlDown = true;
}
}
Here, I'm setting a 'ctrlDown' variable to true.
For the onKeyUp event, you can do the exact opposite:
function document_keyUp(e) {
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if (KeyID == 17) {
ctrlDown = false;
}
}
Then, in the click event of your column elements, you can check if Ctrl has been clicked or not:
function columnElement_click() {
if (ctrlDown != undefined && ctrlDown == true)
alert("Ctrl + Click Received");
return false;
}
Make sure your column click handler returns false. Otherwise, the browser will execute the code, but then navigate to the address in the link's 'href' attribute.
Hope this helps.
(See also: http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html)

Resources