Simulating mouse events

Source: Internet
Author: User
Tags tagname

You can use the following three lines of code to simulate mouse events

var ocreateevt = $D. CreateEvent (' mouseevents ');//Simulate Mouse object
Ocreateevt.initmouseevent (' click ', True,true,document.defaultview,0,0,0,0,0,false,false,false,false,0,null);

$ (ID). dispatchevent (OCREATEEVT);

The more convenient application can use the simulation mouse event to write a keyboard operating system, specifically to achieve

Data structure:varOkeycombination = [//non-composite keys do not block default events{page:"', Key:'ESC'Id:"', desc:'ESC key, cancel cursor'}, {page:"', Key:'m'Id:'Zc_person_index_market', desc:'Market Page'}, {page:"', Key:'N'Id:'zc_person_news', desc:'News'}, {page:"', Key:'C'Id:'Zc_person_chat', desc:'Chat'}, {page:"', Key:'s'Id:'Left_feedback', desc:'Comments'}, {page:"', Key:'F'Id:'Table_furl', desc:'Retractable'}, {page:"', Key:'1'Id:'Person_type1', desc:'1'}, {page:"', Key:'2'Id:'person_type2', desc:'2'}, {page:"', Key:'3'Id:'Person_type3', desc:'3'}, {page:"', Key:'4'Id:'Person_type4', desc:'4'}, {key:'ctrl+1'Id:'Common_chat', page:'Chat', desc:'Chat page Toggle Tab'}, {key:'ctrl+2'Id:'Private_chat', page:'Chat', desc:'Chat page Toggle Tab'}, {key:'ctrl+1'Id:'Hotnews', page:'News', desc:'News page Toggle tab'}, {key:'ctrl+2'Id:'news_final', page:'News', desc:'News page Toggle tab'}, {key:'ctrl+3'Id:'News_base', page:'News', desc:'News page Toggle tab'}, {key:'shift+1'Id:'MARKET_TAB1', page:' Market', desc:'Market page Toggle tab'}, {key:'shift+2'Id:'MARKET_TAB2', page:' Market', desc:'Market page Toggle tab'}, {key:'shift+3'Id:'market_tab3', page:' Market', desc:'Market page Toggle tab'}, {key:'shift+4'Id:'MARKET_TAB4', page:' Market', desc:'Market page Toggle tab'}, {key:'ctrl+1'Id:'Canvas_chart1', page:'Canvas', desc:'CANVAS1 Chart'}, {key:'ctrl+2'Id:'Canvas_chart2', page:'Canvas', desc:'CANVAS4 Chart'}, {key:'ctrl+3'Id:'Canvas_chart3', page:'Canvas', desc:'Canvas9 Chart'}, {key:'ctrl+4'Id:'Canvas_chart4', page:'Canvas', desc:'CANVAS16 Chart'}, {key:'ctrl+5'Id:'Canvas_chart5', page:'Canvas', desc:'CANVAS25 Chart'}]; method: function Combinationkey (key,page,id) {//Keyboard EventsWt.key (key, function (e) {e=Wt.e.fix (e); if(Page.length >0) {e.stop (); }              if(Key = = ='ESC') {clearlist (e);                Cleartips (e);              Document.activeElement.blur (); }Else if(Document.activeElement.tagName = = ='BODY'&& window.location.hash.indexOf (page) >-1&&ID)              {$ (ID). dispatchevent (OCREATEEVT); }Else if(Document.activeElement.tagName = = ='BODY'&&ID)              {$ (ID). dispatchevent (OCREATEEVT);    }        }); } for(varI=0; i<okeycombination.length; i++) {Combinationkey (okeycombination[i].key,okeycombination[i].page,okeycombination[i].id); }
View Code

Attention:

1 event references for elements that need to be emulated need to be valid on the page you are currently loading

2 elements that need to be simulated are not easily accessible to be placed in hidden elements.

3 You may need to block default events when using key combinations, and be aware that blocking the handling of default events should be handled at KeyDown, otherwise it will not work.

4 Current Focus element if it is an input box, you can adjust the mouse focus to document.body

Simulating mouse events

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.