Problem a page has two panels, all have an "add (F2)" button, how to do shortcut key support? Picture signal
First time implementation
The feeling should be very simple, ExtJS provides "Ext.util.KeyMap", easy to do shortcut key support.
code example
Copy Code code as follows:
<reference path= "Ext/ext-all-debug-w-comments.js"/>
Ext.onready (function () {
var viewport = ext.create (' Ext.container.Viewport ', {
Layout: {
Type: ' VBox ',
Align: ' stretch '
},
Padding:10,
Items: [{
Xtype: ' Panel ',
ID: ' Panela ',
Title: ' Shortcut key test a ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. '
}, {
Xtype: ' Panel ',
ID: ' Panelb ',
Title: ' Shortcut key Test B ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. '
}]
});
Ext.create (' Ext.util.KeyMap ', {
Target: ' Panela ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add a ');
Ev.stopevent ();
return false;
}
});
Ext.create (' Ext.util.KeyMap ', {
Target: ' Panelb ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add B ');
Ev.stopevent ();
return false;
}
});
});
Actual results
Open the browser directly after the F2 no effect, open the browser with the mouse click a or B and then press F2 also no effect.
Second Implementation
The original DIV element must increase the tabindex=0 attribute.
code example
Copy Code code as follows:
<reference path= "Ext/ext-all-debug-w-comments.js"/>
Ext.onready (function () {
var viewport = ext.create (' Ext.container.Viewport ', {
Layout: {
Type: ' VBox ',
Align: ' stretch '
},
Padding:10,
Items: [{
Xtype: ' Panel ',
ID: ' Panela ',
Title: ' Shortcut key test a ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}, {
Xtype: ' Panel ',
ID: ' Panelb ',
Title: ' Shortcut key Test B ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}]
});
Ext.create (' Ext.util.KeyMap ', {
Target: ' Panela ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add a ');
Ev.stopevent ();
return false;
}
});
Ext.create (' Ext.util.KeyMap ', {
Target: ' Panelb ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add B ');
Ev.stopevent ();
return false;
}
});
});
Actual results
Open the browser directly after the F2 no effect, open the browser with the mouse click a or B and then press F2 will have effect.
third-time implementation
To resolve the shortcut key without clicking on the div after opening the browser, you need to manually invoke the Foucs () method.
code example
Copy Code code as follows:
<reference path= "Ext/ext-all-debug-w-comments.js"/>
Ext.onready (function () {
var viewport = ext.create (' Ext.container.Viewport ', {
Layout: {
Type: ' VBox ',
Align: ' stretch '
},
Padding:10,
Items: [{
Xtype: ' Panel ',
ID: ' Panela ',
Title: ' Shortcut key test a ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}, {
Xtype: ' Panel ',
ID: ' Panelb ',
Title: ' Shortcut key Test B ',
Tbar: [{
Text: ' Add (F2) '
}],
Frame:true,
Flex:1,
HTML: ' Hello, table or form shown here. ',
Autoel: {
Tag: ' Div ',
tabindex:0
}
}]
});
Ext.create (' Ext.util.KeyMap ', {
Target: ' Panela ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add a ');
Ev.stopevent ();
return false;
}
});
Ext.create (' Ext.util.KeyMap ', {
Target: ' Panelb ',
Key:Ext.EventObject.F2,
Fn:function (key, Ev) {
Alert (' Add B ');
Ev.stopevent ();
return false;
}
});
Ext.get (' Panelb '). focus ();
});
Actual results
Open the browser directly after the F2 has effect, open the browser with the mouse click a or B and then press F2 will have effect.