ExtJS4.2: supported shortcut keys (not as simple as you think)

Source: Internet
Author: User
A page has two panels and each has a [add (F2)] button. How do I support shortcut keys? The figure indicates that the first implementation should be very simple. ExtJs provides Ext. util. KeyMap, which is easy to support shortcut keys. Code example 1 // & lt; referencepath & quot; Ext/ext-all-debu... Synta

A page has two panels and each has a [add (F2)] button. How do I support shortcut keys? Image Diagram

The first implementation should feel very simple. ExtJs provides "Ext. util. KeyMap", which is easy to support shortcut keys. Sample Code
1 ///
2
3 Ext. onReady (function (){
4
5 var viewport = Ext. create ('ext. container. viewport ',{
6 layout :{
7 type: 'vbox ',
8 align: 'stretch'
9 },
10 padding: 10,
11 items :[{
12 xtype: 'panel ',
13 id: 'panela ',
14 title: 'test the keypa ',
15 tbar :[{
16 text: 'add (F2 )'
17}],
18 frame: true,
19 flex: 1,
20 html: 'Hello, the table or form shown here. '
21 },{
22 xtype: 'panel ',
23 id: 'panelb ',
24 title: 'test B 'with a shortcut key ',
25 tbar :[{
26 text: 'add (F2 )'
27}],
28 frame: true,
29 flex: 1,
30 html: 'Hello, the table or form shown here. '
31}]
32 });
33
34 Ext. create ('ext. util. keymap ',{
35 target: 'panela ',
36 key: Ext. EventObject. F2,
37 fn: function (key, ev ){
38 alert ('add ');
39
40 ev. stopEvent ();
41
42 return false;
43}
44 });
45
46 Ext. create ('ext. util. keymap ',{
47 target: 'panelb ',
48 key: Ext. EventObject. F2,
49 fn: function (key, ev ){
50 alert ('add B ');
51
52 ev. stopEvent ();
53
54 return false;
55}
56 });
57}); the actual result is that pressing F2 directly after opening the browser has no effect. Clicking A or B with the mouse and then pressing F2 has no effect.

The second implementation is that the div element must add the tabindex = 0 attribute. Sample Code
1 ///
2
3 Ext. onReady (function (){
4
5 var viewport = Ext. create ('ext. container. viewport ',{
6 layout :{
7 type: 'vbox ',
8 align: 'stretch'
9 },
10 padding: 10,
11 items :[{
12 xtype: 'panel ',
13 id: 'panela ',
14 title: 'test the keypa ',
15 tbar :[{
16 text: 'add (F2 )'
17}],
18 frame: true,
19 flex: 1,
20 html: 'Hello, the table or form shown here. ',
21 autoEl :{
22 tag: 'div ',
23 tabindex: 0
24}
25 },{
26 xtype: 'panel ',
27 id: 'panelb ',
28 title: 'test B with a shortcut key ',
29 tbar :[{
30 text: 'add (F2 )'
31}],
32 frame: true,
33 flex: 1,
34 html: 'Hello, the table or form shown here. ',
35 autoEl :{
36 tag: 'div ',
37 tabindex: 0
38}
39}]
40 });
41
42 Ext. create ('ext. util. keymap ',{
43 target: 'panela ',
44 key: Ext. EventObject. F2,
45 fn: function (key, ev ){
46 alert ('add ');
47
48 ev. stopEvent ();
49
50 return false;
51}
52 });
53
54 Ext. create ('ext. util. keymap ',{
55 target: 'panelb ',
56 key: Ext. EventObject. F2,
57 fn: function (key, ev ){
58 alert ('add B ');
59
60 ev. stopEvent ();
61
62 return false;
63}
64 });
65}); it is ineffective to press F2 directly after opening the browser. After opening the browser, click A or B and then press F2.

The third implementation solves the problem of recognizing the shortcut key without clicking div after opening the browser. You need to manually call the foucs () method. Sample Code
1 ///
2
3 Ext. onReady (function (){
4
5 var viewport = Ext. create ('ext. container. viewport ',{
6 layout :{
7 type: 'vbox ',
8 align: 'stretch'
9 },
10 padding: 10,
11 items :[{
12 xtype: 'panel ',
13 id: 'panela ',
14 title: 'test the keypa ',
15 tbar :[{
16 text: 'add (F2 )'
17}],
18 frame: true,
19 flex: 1,
20 html: 'Hello, the table or form shown here. ',
21 autoEl :{
22 tag: 'div ',
23 tabindex: 0
24}
25 },{
26 xtype: 'panel ',
27 id: 'panelb ',
28 title: 'test B with a shortcut key ',
29 tbar :[{
30 text: 'add (F2 )'
31}],
32 frame: true,
33 flex: 1,
34 html: 'Hello, the table or form shown here. ',
35 autoEl :{
36 tag: 'div ',
37 tabindex: 0
38}
39}]
40 });
41
42 Ext. create ('ext. util. keymap ',{
43 target: 'panela ',
44 key: Ext. EventObject. F2,
45 fn: function (key, ev ){
46 alert ('add ');
47
48 ev. stopEvent ();
49
50 return false;
51}
52 });
53
54 Ext. create ('ext. util. keymap ',{
55 target: 'panelb ',
56 key: Ext. EventObject. F2,
57 fn: function (key, ev ){
58 alert ('add B ');
59
60 ev. stopEvent ();
61
62 return false;
63}
64 });
65
66 Ext. get ('panelb'). focus ();
67 });

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.