Question about Java global shortcuts

Source: Internet
Author: User

From: http://ttnecro.javaeye.com/blog/656671

 

Friends who have played DNF know that they must constantly press the attack key during continuous attacks, and cannot press and hold the button for automatic combo. It is obvious that this will greatly damage the keyboard, and the hands are also very tired. There are also many plug-ins on the Internet, but they all have viruses. Then, I came up with the idea of customizing a DNF plug-in. I found some code about the robot from Cao Lisheng (partner) and started.

The idea at the beginning was to set the DNF attack key to the E key, which is the previous habit key, so when you press and hold the X key, you can use the robot to keep pressing the release e key. In this way, keyadapter can be easily implemented.

However, the problem was quickly discovered, and the keyadapter was limited, so a component got the focus. After the feature was turned on, the main program could not be listened to when it switched to DNF.

After this idea failed, Cao Lisheng reminded me of the global shortcut key.

 

However, there is also a problem. This so-called global shortcut is not limited to getting the focus of a component. It cannot work after the main program is switched. You cannot press CTRL + ALT + Z like QQ. So it still ended in failure at the end... but I heard that C ++ can be implemented, and I will try again later...Import Java. AWT. awtevent; <br/> Import Java. AWT. awtexception; <br/> Import Java. AWT. toolkit; <br/> Import Java. AWT. event. awteventlistener; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import Java. AWT. event. keyevent; <br/> Import javax. swing. jbutton; <br/> public class dnfrobot implements runnable, actionlistener {<br/> private Java. AWT. robot robot; <br/> private int key; <br/> private Boolean ispress = false; <br/> private jbutton control; <br/> private Boolean pressx = false; <br/>/** <br/> * listener for awteventlistener <br/> */<br/> class caplistener implements awteventlistener {<br/> Public void eventdispatched (awtevent event) {<br/> If (event. getclass () = keyevent. class) {<br/> keyevent E = (keyevent) event; <br/> key = E. getkeycode (); <br/> system. out. println ("button" + key); <br/> If (E. GETID () = keyevent. key_pressed) {<br/> keypressed (E); <br/>} else if (E. GETID () = keyevent. key_released) {<br/> keyreleased (E ); <br/>}< br/> Public void keypressed (keyevent event) {<br/> If (event. getkeycode () = keyevent. vk_x) {<br/> pressx = true; <br/>}< br/> Public void keyreleased (keyevent event) {<br/> If (event. getkeycode () = keyevent. vk_x) {<br/> pressx = false; <br/>}< br/>/** <br/> * execution method <br/> */<br/> Public void function () {<br/> try {<br/> robot = new Java. AWT. robot (); <br/>}catch (awtexception E1) {<br/> e1.printstacktrace (); <br/>}< br/> while (ispress) {<br/> If (pressx) {<br/> system. out. println ("press the X key"); <br/> try {<br/> robot. keypress (Java. AWT. event. keyevent. vk_e); <br/> system. out. println ("press the e key in the background"); <br/> thread. sleep (100); <br/> robot. keyrelease (Java. AWT. event. keyevent. vk_e); <br/> system. out. println ("Release e key in the background"); <br/> thread. sleep (100); <br/>} catch (interruptedexception E1) {<br/> e1.printstacktrace (); <br/>}< br/>/** <br/> * rewrite the run method <br/> */<br/> Public void run () {<br/> toolkit = toolkit. getdefatooltoolkit (); <br/> toolkit. addawteventlistener (New caplistener (), awtevent. key_event_mask); <br/> function (); <br/>}< br/>/** <br/> * method for event listening <br/> */<br/> Public void actionreceivmed (actionevent E) {<br/> control = (jbutton) E. getsource (); <br/> string S = E. getactioncommand (); <br/> If (S. equals ("START") {<br/> ispress = true; <br/> thread th = new thread (this); <br/> th. start (); <br/> control. settext ("stop"); <br/>}< br/> If (S. equals ("stop") {<br/> ispress = false; <br/> control. settext ("START"); <br/>}< br/> If (S. equals ("exit") {<br/> system. exit (0); <br/>}< br/> 

 

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.