linux上的Java机器人——没有大写字符(使用x11vnc)

时间:2021-02-24 22:07:43

I am trying to simulate keyboard on my vps server with debian lenny using Java Robot Class. I am watching results using these commands (to connect with VNCviewer):

我正在尝试使用Java Robot类在我的vps服务器上模拟键盘。我正在使用以下命令查看结果(连接到VNCviewer):

Xvfb :2 -screen 0 1366x768x16 & fluxbox & x11vnc -display :2 -bg -usepw -nomodtweak -xkb

I can type lower case letters but it doesn't work with capital letters. I used following code from the internet + my modifications:

我可以打小写字母,但不能打大写字母。我使用了以下来自互联网的代码+我的修改:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class TypingRobot extends Robot {

public TypingRobot() throws AWTException {
    super();
    // TODO Auto-generated constructor stub
}

public void keyType(int keyCode) {
 keyPress(keyCode);
 delay(50);
 keyRelease(keyCode);
}

public void keyType(int keyCode, int keyCodeModifier) {

 keyPress(keyCodeModifier);
 keyPress(keyCode);  
 delay(50);
 keyRelease(keyCode);
 keyRelease(keyCodeModifier);
}


public void type(String text) {
  //String textUpper = text.toUpperCase();
  String textUpper = text;

  for (int i=0; i<text.length(); ++i) {
   typeChar(textUpper.charAt(i));
  }  
 }

 private void typeChar(char c) {
  boolean shift = true;
  int keyCode;


  switch (c) {
  case '~':
   keyCode = (int)'`';
   break;
  case '!':
   keyCode = (int)'1';
   break;
  case '@':
   keyCode = (int)'2';
   break;
  case '#':
   keyCode = (int)'3';
   break;
  case '$':
   keyCode = (int)'4';
   break;
  case '%':
   keyCode = (int)'5';
   break;
  case '^':
   keyCode = (int)'6';
   break;
  case '&':
   keyCode = (int)'7';
   break;
  case '*':
   keyCode = (int)'8';
   break;
  case '(':
   keyCode = (int)'9';
   break;
  case ')':
   keyCode = (int)'0';
   break;
  case ':':
   keyCode = (int)';';
   break;
  case '_':
   keyCode = (int)'-';
   break;
  case '+':
   keyCode = (int)'=';
   break;
  case '|':
   keyCode = (int)'\\';
   break;
  case '?':
   keyCode = (int)'/';
   break;
  case '{':
   keyCode = (int)'[';
   break;
  case '}':
   keyCode = (int)']';
   break;
  case '<':
   keyCode = (int)',';
   break;
  case '>':
   keyCode = (int)'.';
   break;
  case '/':
   keyCode = 47;
   shift = false;
   break;
  case ' ':
   keyCode = (int)' ';
   shift = false;
   break;
  case '.':
   keyCode = (int)'.';
   shift = false;
   break; 
  default:
   if( (int)c>=65 && (int)c<=90 ){
       keyCode = (int)c;
   }
   else{
   keyCode = (int)c-32;
   shift = false;
   }
  }
  if (shift)
   keyType(keyCode, KeyEvent.VK_SHIFT);
  else
   keyType(keyCode);
 } }

My input looks like:

我输入的样子:

robot.type("Some string..."); 

but it changes capital letters to small ones. I think there is some problem with my server because Windows version of this code worked well for me. I can press SHIFT on VNCviewer and it works but probably Robot/x11vnc or something related can't press it for me.

但它将大写字母换成了小写字母。我认为我的服务器有一些问题,因为这个代码的Windows版本对我来说工作得很好。我可以在VNCviewer上按SHIFT,它可以工作,但是机器人/x11vnc或者其他相关的东西可能不能帮我按。

Any ideas how to enable SHIFT key? (I tested CAPSLOCK too, nothing happened too)

有什么办法可以实现SHIFT键吗?(我也测试了CAPSLOCK,什么都没发生)

1 个解决方案

#1


2  

The answer is - do not use x11vnc, shift/ctrl/alt keys not working there. Use tightvncserver instead.

答案是——不要使用x11vnc、shift/ctrl/alt键。使用tightvncserver代替。

#1


2  

The answer is - do not use x11vnc, shift/ctrl/alt keys not working there. Use tightvncserver instead.

答案是——不要使用x11vnc、shift/ctrl/alt键。使用tightvncserver代替。