How to keep pressed the SHIFT button on the keyboard?

29 ビュー (過去 30 日間)
Adriano
Adriano 2025 年 1 月 8 日 10:38
コメント済み: Jacob Mathew 2025 年 1 月 8 日 16:11
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 日 11:06
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 日 13:35
Tanks for your answer Jacob, but the output is the same.
Jacob Mathew
Jacob Mathew 2025 年 1 月 8 日 16:11
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

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by