Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending slash and backslash with Java

Tags:

java

awtrobot

When using a Java robot, when sending a slash or backslash throws an exception.

For example:

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);
}

Then, when I want to type those, I use:

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."); }


}

I get two error messages in my console. By the way, all other keystrokes I tried to send worked fine.

I get the follwowing stacktrace for slash:

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)

Backslashes work, but also throw an 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)

Is there any way I can send slash and backslash keystrokes?

like image 335
borges Avatar asked Dec 21 '12 15:12

borges


People also ask

What is the backslash character used for in Java?

The backslash character is a special character in that it is used to trigger an escape sequence. The escape sequence can then print special characters like the quotes and the backslash. Thus, the following code can show “\” in a String in Java:

How do you print a back slash in Java?

The forward slash should print. The back slash should be proceeded by the escape character ‘\’. Yes that is another back slash. Search Java escape characters and print them out and tape it to the side of your display. They will come in handy!

What is a backward slash in a number in Java?

Backward slash is a escape character in Java. So if you write 2 backward slash then only 1 slash is considered. Let’s take a example, Chinni..!! How do you write a program to display a number from 1 to n using recursion in C? // 'static' so that the value of i is not lost after each recursion

Is there a difference between a backslash and a slash?

No, there isn't. Slashes work everywhere, backslashes work only on Windows and are a pain to type and read. They are used only by people who mistakenly think they have to use them.


2 Answers

Okay, I have found a solution that may be handy for users working with different keyboard layouts. It uses Alt codes.

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);

}

Then you call it like this:

For back slashes: alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD9, KeyEvent.VK_NUMPAD2);

For normal ones: alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);

No exceptions. Also works for all other characters. But make sure you have Num Lock on when using this.

like image 200
borges Avatar answered Nov 15 '22 12:11

borges


(Sorry I should mention this isn't quite an answer, just saying that it does work for me so I'm guessing it's a configuration issue or something--but I thought that it was something others might find handy. It should be a comment but for technical reasons I have to make it an answer)

I hadn't messed with the Robot class and just spent a FUN half hour messing around and bulding some stuff on top of this cool java class.

For me sending slash and backslash work fine. Since the VK_??? stuff maps pretty well to the ASCII characters, you can send '\' or '/' and it should work too.

I used Groovy because that's what I play in these days, but here's a nice example and a bunch of reusable code I just came up with. It's written as a script but could easily be converted to a class in either Groovy or Java (and I will do so soon).

This must be run from a "Priviliged" shell (for instance right-click on the command prompt and select "Run as administrator").

also it MUST have time for you to let go of the keyboard! (Learned that the hard way), so if you use groovyShell and use alt-r to run it, be sure to put a 1 second delay before sending the first keys or your ALT will become part of the keys pressed.

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)
}
like image 43
Bill K Avatar answered Nov 15 '22 14:11

Bill K