2013-06-18 53 views
1

我正在嘗試使機器人打字方法變得更簡單。 KeyEvent中的大部分鍵碼都是十六進制代碼。字符到十六進制參數

安排:

  1. Stringchar[]
  2. 的for-each通過(int)char
  3. 按鍵(的(int)char十六進制值)
  4. keyRelease(的十六進制值的char[]
  5. 查找十六進制值(int)char

我有什麼至今:

import java.awt.*; 
import java.awt.event.*; 
public class Driver 
{ 
    private static Robot r; 
    public static void send(String phrase) 
    { 
     char[] chars = phrase.toCharArray(); 
     for (char letter:chars) 
     { 
      //int hex = hex value of (int)letter 
      //r.keyPress(hex); 
      //r.keyRelease(hex); 
     } 
    } 
    public static void main(String[]args) 
    { 
     try 
     { 
      r = new Robot(); 
      r.delay(5000); 
      send("Hello World"); 
     } 
     catch(AWTException e) 
     { 
      //Nothing 
     } 
    } 
} 
+2

請註明具體問題與您的代碼是一樣的。你想實現你的代碼目前沒有做什麼? – fge

+0

你需要一個十六進制字符串還是隻需要一個整數值?如果您使用ASCII字符,則可以將char轉換爲int。 –

+0

keyPress需要採取一個看起來像0x41的參數來按A我需要找出一種方法來將字符轉換爲十六進制。我無法傳遞一個字符串。所以它應該看起來像keyPress(0x4b); –

回答

1

鍵碼爲ASCII字母是等於他們的大寫字符值。 所以,你可以做到以下幾點:

​​

在這種循環中,字符串 「Hello World」 將給 「世界你好」。 它不適用於感嘆號等內容。

此外,如果你wan't機器人發送大寫字母,你將不得不模擬上的Shift鍵或在大寫鎖定鍵的新聞。

不確定這種方法是否可靠。你可以做很多if/else(或開關)從java.awt.event.KeyEvent返回正確的鍵碼常量。

+0

簽名不完全是這樣回答的我但是我感覺這是最接近的,我真的只想知道如何傳遞一個十六進制作爲參數keyPress(0x4b)例如 –

+0

0x4b是一個完全可用的值,但它是?一樣書寫75(int)的「K」 –

+0

因此,如何將使用+「K」交易我得到一個字符爲十六進制值 –

1

這項工作,但它有特殊字符,如「你好世界權證」的問題,請參見輸出。
它最好使用\ unnnn UniCode範例。

 final char[] chars = phrase.toCharArray(); 
     for (final char c : chars) { 
      try { 
       final byte[] tabB = (c + "").getBytes("UTF-8"); 
// You can replace "UTF-8" by : System.getProperty("sun.jnu.encoding"), 
       for (final byte element : tabB) { 
        System.out.format("%s\t0x%s\n", element, 
          Integer.toHexString(element)); 
       } 
      } catch (final UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
      } 
     } 

輸出:

72 0x48 
101 0x65 
108 0x6c 
108 0x6c 
111 0x6f 
32 0x20 
87 0x57 
111 0x6f 
114 0x72 
108 0x6c 
100 0x64 
32 0x20 
-61 0xffffffc3 
-87 0xffffffa9 
32 0x20 
-61 0xffffffc3 
-89 0xffffffa7 

與System.getProperty( 「sun.jnu.encoding」) - >我的電腦ISO-8859-15

72 0x48 
101 0x65 
108 0x6c 
108 0x6c 
111 0x6f 
32 0x20 
87 0x57 
111 0x6f 
114 0x72 
108 0x6c 
100 0x64 
32 0x20 
-23 0xffffffe9 
32 0x20 
-25 0xffffffe7 
相關問題