How to keep pressed the SHIFT button on the keyboard?
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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!
0 comentarios
Respuestas (1)
  Jacob Mathew
      
      
 el 8 de En. de 2025
        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 comentarios
  Jacob Mathew
      
      
 el 8 de En. de 2025
				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
Ver también
Categorías
				Más información sobre Platform and License en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

