How to keep pressed the SHIFT button on the keyboard?

조회 수: 4 (최근 30일)
Adriano
Adriano 2025년 1월 8일
댓글: Jacob Mathew 2025년 1월 8일
Hi all! I have two monitors and I need to pass the browser window form the left monitor to the right monitor. The shortcut to do this on the keyboard is: WINDOW + SHIFT + RIGHT ARROW. If i press phisically those 3 buttons on the keyboard the windows is correctly moved to the right monitor, however, if I try to do the same on MATLAB with this code:
import java.awt.*;
import java.awt.event.*;
rob=Robot;
[stat, h, url] = web('https://www.google.com','-new');
pause(5)
rob.keyPress(KeyEvent.VK_WINDOWS);
rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_RIGHT);
rob.keyRelease(KeyEvent.VK_RIGHT);
rob.keyRelease(KeyEvent.VK_SHIFT);
rob.keyRelease(KeyEvent.VK_WINDOWS);
The output is like I press WINDOW + RIGHT ARROW. So, seems like the SHIFT button is pressed and released before the RIGHT ARROW button. Someone can help me? Many thanks!

답변 (1개)

Jacob Mathew
Jacob Mathew 2025년 1월 8일
Hey Adriano,
You can add a small delay between the keyPress and keyRelease functions to let them register as a simulatneously pressed. The following code modifies that:
import java.awt.*;
import java.awt.event.*;
rob=Robot;
% Warning: [STAT,H,URL] = WEB(___) does not return a handle or URL for pages that open in the system
% browser. Use STAT = WEB(___) instead
stat = web('https://www.google.com','-new');
pause(5) % Delay to let the web page open
rob.keyPress(KeyEvent.VK_WINDOWS);
rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_RIGHT);
rob.delay(100); % This delay lets the 3 key input be detected as near simulataneous
rob.keyRelease(KeyEvent.VK_RIGHT);
rob.keyRelease(KeyEvent.VK_SHIFT);
rob.keyRelease(KeyEvent.VK_WINDOWS);
  댓글 수: 2
Adriano
Adriano 2025년 1월 8일
Tanks for your answer Jacob, but the output is the same.
Jacob Mathew
Jacob Mathew 2025년 1월 8일
Hey Adriano,
Could you check the attached video in the zip file? Is that the behavior you are expecting? That is the output of running the code at my end

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

카테고리

Help CenterFile Exchange에서 Platform and License에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by