Java Robot - Unable to perform SHIFT-RIGHT

조회 수: 5 (최근 30일)
Philip M
Philip M 2021년 3월 23일
답변: Gautam 2025년 1월 9일
I'm trying to use the Java robot to do a page-right command which, in the program in I'm trying to use it for, can be performed with a SHIFT-RIGHT keyboard input. This is essentialy the code I'm using:
import java.awt.*
import java.awt.event.*
rob=Robot;
rob.keyPress(KeyEvent.VK_SHIFT)
rob.keyPress(KeyEvent.VK_RIGHT)
rob.keyRelease(KeyEvent.VK_RIGHT)
rob.keyRelease(KeyEvent.VK_SHIFT)
However, when running the code, the shift key is pressed but appears to essentially be released before the right arrow is pressed, so I only get a single movement to the right instead of a page-right. Replacing VK_RIGHT with VK_A will correctly type an uppercase A as expected, so there seems to be some issue with combining the SHIFT key with non-character keys. I tried adding an autoDelay and adding long pauses between key events, but it didn't fix the issue.
I can of course program it to press the right arrow multiple times, but that's messier and slower and involves an extra step where I have to determine how many right arrow presses it takes to do a page-right.
Any ideas what I can do?
  댓글 수: 1
Adriano
Adriano 2025년 1월 8일
I have the same problem...did you fix it?

댓글을 달려면 로그인하십시오.

답변 (1개)

Gautam
Gautam 2025년 1월 9일
Hello Philip M,
You can introduce a brief delay between the "keypress" and "keyRelease" functions to ensure they are recognized as being pressed simultaneously.
rob.keyPress(KeyEvent.VK_SHIFT)
rob.keyPress(KeyEvent.VK_RIGHT)
rob.delay(100);
rob.keyRelease(KeyEvent.VK_RIGHT)
rob.keyRelease(KeyEvent.VK_SHIFT)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by