Home > Web Front-end > JS Tutorial > About ExtJS4.1: Problems with shortcut key support_javascript skills

About ExtJS4.1: Problems with shortcut key support_javascript skills

WBOY
Release: 2016-05-16 17:35:45
Original
1380 people have browsed it

Question: A page has two panels, both of which have an [Add (F2)] button. How to support shortcut keys? Picture indication

The first implementation
It feels like it should be very simple. ExtJs provides "Ext.util.KeyMap", which makes it easy to support shortcut keys.
Code example

Copy code The code is 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)'
                                                                                                                                                                                                                                           xtype: 'panel',
id: 'panelB',
title: 'Shortcut key test B',
tbar: [{
text: 'Add (F2)'
}],
frame: true,
flex: 1,
html: 'Hello, the 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 result

After opening the browser Pressing F2 directly has no effect; opening the browser and clicking A or B with the mouse and then pressing F2 also has no effect.

The second implementation

It turns out that the div element must add the attribute tabindex=0.
Code example


Copy code

The code is as follows:

///

Ext.onReady(function () {

var viewport = Ext.create('Ext.container.Viewport', {
layout: {
type: 'vbox',
align: 'stretch'
},
padding : 10,
items: [{
bar: [{
text: 'Add (F2)'
          }],
                           frame: true,                                                                                                                                                                                . autoEl : {
tag: 'div',
tabindex: 0
}
}, {
XType: 'Panel',
ID: 'Panelb',
Title: 'Shortcut key test B',
tbar: [{
text: 'Add (F2)'
  }],
frame: true,
 flex: 1,
html: 'Hello, the table or form shown here. ',
                                                                                                                                                      }]
});

Ext.create('Ext.util.KeyMap', {
target: 'panelA',
key: Ext.EventObject.F2,
fn: function (key, ev) {
          alert('Add A'); 🎜> Ext. Create ('EXT.UTIL.KEYMAP', {
Target: 'Panelb',
Key: Ext.eventobject.f2,
Fn: Function (key, EV) {
Alert ('Add to B');

ev.stopEvent();

return false;
}
});
});


Actual results
Pressing F2 directly after opening the browser has no effect; opening the browser and clicking A or B with the mouse and then pressing F2 will have the effect.

The third implementation
To solve the problem of recognizing shortcut keys without clicking the div after opening the browser, you need to manually call the foucs() method.
Code example
Copy code The code is as follows:

///

Ext.onReady(function () {

var viewport = Ext.create('Ext.container.Viewport', {
layout: {
type: 'vbox',
align: 'stretch'
},
padding : 10,
items: [{
bar: [{
text: 'Add (F2)'
          }],
                           frame: true,                                                                                                                                                                                . autoEl : {
tag: 'div',
tabindex: 0
}
}, {
XType: 'Panel',
ID: 'Panelb',
Title: 'Shortcut key test B',
tbar: [{
text: 'Add (F2)'
  }],
frame: true,
 flex: 1,
html: 'Hello, the table or form shown here. ',
                                                                                                                                                      }]
});

Ext.create('Ext.util.KeyMap', {
target: 'panelA',
key: Ext.EventObject.F2,
fn: function (key, ev) {
          alert('Add A'); 🎜> Ext. Create ('EXT.UTIL.KEYMAP', {
Target: 'Panelb',
Key: Ext.eventobject.f2,
Fn: Function (key, EV) {
Alert ('Add to B');

ev.stopEvent();

return false;
}
});

Ext.get('panelB'). focus();
});


Actual results
Open the browser and press F2 directly to have the effect; open the browser and click A or B with the mouse and then press F2 It works.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template