2012-12-21 5 views
11

के साथ स्लैश और बैकस्लैश भेजना जावा रोबोट का उपयोग करते समय, स्लैश या बैकस्लैश भेजते समय अपवाद फेंकता है।जावा

उदाहरण के लिए:

public void slash() throws AWTException { 
    Robot rob = new Robot(); 
    rob.keyPress(KeyEvent.VK_SLASH); 
    rob.keyRelease(KeyEvent.VK_SLASH); 
} 

public void backSlash() throws AWTException { 
    Robot rob = new Robot(); 
    rob.keyPress(KeyEvent.VK_BACK_SLASH); 
    rob.keyRelease(KeyEvent.VK_BACK_SLASH); 
} 

तब, जब मैं उन टाइप करना चाहते हैं, मैं का उपयोग करें:

public void type() { 

    try { 
     slash(); 
    } catch (AWTException e) { System.out.println("Exception when typing slash."); } 

    try { 
     backSlash(); 
    } catch (AWTException e) { System.out.println("Exception when typing back slash."); } 


} 

मैं अपने कंसोल में दो त्रुटि संदेश मिलता है। वैसे, मैं भेजने के लिए कोशिश की सभी अन्य कीस्ट्रोक ठीक काम किया।

मैं स्लेश के लिए follwowing स्टैकट्रेस मिलती है:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code 
    at sun.awt.windows.WRobotPeer.keyPress(Native Method) 
    at java.awt.Robot.keyPress(Unknown Source) 
    at com.paschoalinoto.bruno.pastescript.Paste.slash(Paste.java:23) 
    at com.paschoalinoto.bruno.pastescript.Paste.type(Paste.java:36) 
    at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

बैकस्लैश काम करते हैं, लेकिन यह भी एक IllegalArgumentException फेंक:

java.lang.IllegalArgumentException: Invalid key code 
at sun.awt.windows.WRobotPeer.keyPress(Native Method) 
at java.awt.Robot.keyPress(Unknown Source) 
at com.paschoalinoto.bruno.pastescript.Paste.press(Paste.java:198) 
at com.paschoalinoto.bruno.pastescript.Paste.paste(Paste.java:173) 
at com.paschoalinoto.bruno.pastescript.Paste.finalPaste(Paste.java:227) 
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$200(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

वहाँ किसी भी तरह से मैं स्लेश और बैकस्लैश कीस्ट्रोक्स भेज सकते है?

+9

कृपया * हमेशा * लागू होने पर 'e.printStackTrace()' के परिणाम शामिल करें। – Vulcan

+2

अच्छी तरह से प्रिंट करने की बजाय * एक अपवाद था, आप अपवाद विवरण क्यों मुद्रित नहीं करते? –

+0

ठीक है, मैं इसे जोड़ दूंगा। –

उत्तर

4

ठीक है, मुझे एक समाधान मिला है जो विभिन्न कीबोर्ड लेआउट के साथ काम करने वाले उपयोगकर्ताओं के लिए आसान हो सकता है। यह Alt कोड का उपयोग करता है।

public static void alt(int event1, int event2, int event3, int event4) throws Exception { 

    Robot bot = new Robot(); 
    bot.delay(50); //Optional 
     bot.keyPress(KeyEvent.VK_ALT); 

      bot.keyPress(event1); 
      bot.keyRelease(event1); 

      bot.keyPress(event2); 
      bot.keyRelease(event2); 

      bot.keyPress(event3); 
      bot.keyRelease(event3); 

      bot.keyPress(event4); 
      bot.keyRelease(event4); 

     bot.keyRelease(KeyEvent.VK_ALT); 

} 

तो फिर तुम इसे इस तरह कहते हैं:

वापस लिए स्लैश: alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD9, KeyEvent.VK_NUMPAD2);

सामान्य लोगों के लिए: alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);

कोई छूट नहीं। अन्य सभी पात्रों के लिए भी काम करता है। लेकिन सुनिश्चित करें कि इसका उपयोग करते समय आपके पास न्यू लॉक है।

+1

+1, रचनात्मक सोच। इसे स्वीकार करने के रूप में चिह्नित करें यदि यह आपका अंतिम समाधान है। – Vulcan

+0

हालांकि सुधार के लिए, विधि में प्रत्येक कॉल के लिए एक नया 'रोबोट' का उपयोग न करें। – Vulcan

+0

@ वल्कन यह एक अच्छा विचार है ... वैसे, +1 –

2

(खेद है कि मैं उल्लेख करना चाहिए यह काफी एक जवाब नहीं है, बस कह रही है कि इसके लिए मुझे इतना मेरा अनुमान है कि यह एक विन्यास मुद्दा या कुछ काम करता है - लेकिन मैंने सोचा कि यह कुछ अन्य लोगों के काम मिल सकती थी। यह एक टिप्पणी होनी चाहिए लेकिन तकनीकी कारणों से मुझे इसे उत्तर देना होगा)

मैंने रोबोट वर्ग से गड़बड़ नहीं की थी और बस एक शांत आधा घंटा बिताया और इस शांत जावा क्लास के ऊपर कुछ सामानों को बल्ब किया ।

मेरे लिए स्लैश और बैकस्लैश काम ठीक भेज रहा है। वीके _ के बाद से ??? सामान ASCII वर्णों के लिए बहुत अच्छी तरह से मानचित्र हैं, आप '\' या '/' भेज सकते हैं और इसे भी काम करना चाहिए।

मैंने ग्रोवी का उपयोग किया क्योंकि मैं इन दिनों में यही खेलता हूं, लेकिन यहां एक अच्छा उदाहरण है और पुन: प्रयोज्य कोड का एक गुच्छा मैं अभी आया हूं। इसे एक स्क्रिप्ट के रूप में लिखा गया है लेकिन आसानी से ग्रोवी या जावा में कक्षा में परिवर्तित किया जा सकता है (और मैं जल्द ही ऐसा करूंगा)।

यह "प्रीविलिज्ड" खोल से चलाया जाना चाहिए (उदाहरण के लिए कमांड प्रॉम्प्ट पर राइट-क्लिक करें और "व्यवस्थापक के रूप में चलाएं" चुनें)।

भी आपके पास कीबोर्ड के जाने के लिए समय होना चाहिए! (कठिन तरीका सीख लिया गया), इसलिए यदि आप groovyShell का उपयोग करते हैं और इसे चलाने के लिए alt-r का उपयोग करते हैं, तो पहली कुंजी भेजने से पहले 1 सेकंड देरी डालना सुनिश्चित करें या आपका एएलटी दबाए गए कुंजी का हिस्सा बन जाएगा।

 
import java.awt.* 
import java.awt.event.* 
import static java.awt.event.KeyEvent.* 

r=new Robot() 
r.autoWaitForIdle = true 
r.autoDelay=200 // Usually works with 0 but sometimes that's too fast. 

// This will alt-tab you to your "Previous" app. While testing I edited this in notepad++ 
// then tabbed out to a shell to execute it, this tabbed back into my editor and typed 
// the "test" text. 
alt VK_TAB 

send "backslash=\\ \nforward slash =/" 

// This will send any string 
def send(String s) 
{ 
    def difference = ("a" as Character) - ("A" as Character) 
    s.each { 
     Character c=it as Character 
     if(c.isUpperCase()) { 
      shift c 
     } else if(c.isLowerCase()) { 
      send(c - difference) 
     } 
     else send(c) 
    } 
} 

// These will work for integers and chars, NOT strings 
def send(key) 
{ 
    press(key as Integer) 
    release(key as Integer) 
} 
def alt(key) 
{ 
    press VK_ALT 
    send key 
    release VK_ALT 
} 
def shift(key) 
{ 
    press VK_SHIFT 
    send key 
    release VK_SHIFT 
} 

def press(key) 
{ 
    r.keyPress(key as Integer) 
} 
def release(int key) 
{ 
    r.keyRelease(key as Integer) 
}