English 中文(简体)
Problems using key emulator in Java
原标题:

I am writing some Java code so that in the code, when an event happens, it opens Microsoft PowerPoint from the program and then emulates some key presses which are defined in the code. My problem is that when I ask it to emulate a key press and pass in the decimal value of the key I want it to emulate, it does it wrong. The code is as follows:

public void test(String key) throws Exception {

    int value = (int)key.charAt(0);

    Controller c = new Controller();
    Executer e = new Executer(c);

    e.exec(c,"POWERPNT");

    c.delay(5000);
    c.emulateKeyTyped(97);
    c.emulateKeyTyped(98);
}

The code above is meant to open Microsoft PowerPoint and emulate the keys a and b (whose ASCII values are 97 and 98 ), but instead PowerPoint prints 1 and 2 and I have no idea why this is. This is using PowerPoint 2007. The odd thing is that if I replace the 97 by "KeyEvent.VK__A" (which is the same integer, ie. 97 , since "KeyEvent.VK_A" returns an integer) then it prints the letter a fine in PowerPoint. The reason I want to use integers is because it is being passed in from another part of the program and also because I want to be able to emulate key presses other than just letters/numbers etc. (Also arrows etc.)

I m not sure if the problem is in the code or if it something to do with the PowerPoint 2007, but any help would be much appreciated.


Thanks for the answers so far,

This works for a through to z but I still can t get it to work for other values such as ? etc.

问题回答

According to the documentation:

VK_A thru VK_Z are the same as ASCII A thru Z (0x41 - 0x5A)

The integer values for the KeyEvent constants for the alphabetic keys are the ASCII values for the uppercase letters not the lowercase ones.

This means you want to use 65 and 66 not 97 and 98.

The value of VK_A is ox41 = 65. Hence, if you modify your code as:

c.emulateKeyTyped(65);
c.emulateKeyTyped(66);

then it should work fine. Note this is just a logical conclusion from what you have written above, I don t know an iota about what a Controller or an Executor is!!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签