I am interested in simulating a keyboard input using .m code.
How would I go about simulating typing and other keyboard inputs?
Similar to this, but with the keyboard instead of the mouse.
Thanks,
Kevin

 채택된 답변

Walter Roberson
Walter Roberson 2011년 4월 11일

1 개 추천

Java Robot class.

댓글 수: 7

Paulo Silva
Paulo Silva 2011년 4월 11일
%Example
import java.awt.Robot;
import java.awt.event.*;
SimKey=Robot;
SimKey.keyPress(KeyEvent.VK_P);
Matt Fig
Matt Fig 2011년 4월 11일
I like this example better:
import java.awt.Robot;
import java.awt.event.*;
SimKey=Robot;
SimKey.keyPress(KeyEvent.VK_W);
SimKey.keyPress(KeyEvent.VK_H);
SimKey.keyPress(KeyEvent.VK_Y);
SimKey.keyPress(KeyEvent.VK_ENTER);
Paulo Silva
Paulo Silva 2011년 4월 11일
import java.awt.Robot;
import java.awt.event.*;
SimKey=Robot;
SimKey.keyPress(KeyEvent.VK_S);
SimKey.keyPress(KeyEvent.VK_P);
SimKey.keyPress(KeyEvent.VK_Y);
SimKey.keyPress(KeyEvent.VK_ENTER);
Paulo Silva
Paulo Silva 2011년 4월 11일
execute my code and Matt code on the command line of matlab (not in a script) :)
Walter Roberson
Walter Roberson 2011년 4월 11일
In theory shouldn't you also be sending virtual key release events?
Walter Roberson
Walter Roberson 2012년 9월 9일
ying xing FENG commented "It works for text. But not for other active windows other than Matlab. For eg. on screen key borad. Any idea? "

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

추가 답변 (5개)

Garrett Cassidy
Garrett Cassidy 2019년 8월 14일

1 개 추천

Does anyone know how to emulate a ; or a \ ? I can emulate keystrokes, but the ‘VK_OEM_1´input to keyPress is undefined.
Thanks
Kevin
Kevin 2011년 4월 11일

0 개 추천

Haha, nice.
Why does SimKey.keyPress(KeyEvent.VK_w); now work for undercase w? Is there a full list of keyboard inputs I can reference?
What is "VK" referencing to?

댓글 수: 2

Paulo Silva
Paulo Silva 2011년 4월 11일
편집: dpb 2020년 7월 5일
Jan
Jan 2011년 4월 11일
KeyEvent.VK_W is the [W] key - there is no upper or lower case *key*! You need the [Shift] key to create uppercase characters:
robot.keyPress(java.awt.event.KeyEvent.VK_SHIFT)
robot.keyPress(java.awt.event.KeyEvent.VK_W)
robot.keyRelase(java.awt.event.KeyEvent.VK_W)
robot.keyRelease(java.awt.event.KeyEvent.VK_SHIFT)
Remember: It is only a robot and it s(t)imulates the keyboard...

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

ali
ali 2011년 11월 30일

0 개 추천

Hi Paulo,
regarding"execute my code and Matt code on the command line of matlab (not in a script) :)"
I am going to kill an running script through simulating Ctrl+C which is written as a function like this:
import java.awt.Robot; import java.awt.event.*; SimKey=Robot; SimKey.keyPress(java.awt.event.KeyEvent.VK_CONTROL); SimKey.keyPress(java.awt.event.KeyEvent.VK_C); SimKey.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
how can I call it? when I call this function it does not work? Thanks, Ali
Amirhosein Ghenaati
Amirhosein Ghenaati 2014년 12월 5일

0 개 추천

hello i want to execute kmplayer then first launch kmplayer then use java robot to push space it will open media folder. my program stop when kmp starts and and when i terminates kmp space chracter will be printed in editor. here is my simple program and i dont know whats problem please help me in this situation
import java.awt.Robot
import java.awt.event.*
keys = Robot;
keys.setAutoDelay(1000)
[status currdir] = system('cd C:\Users\IranZamin\Documents\MATLAB\temporary');
[status result] = system('KMPlayer.lnk');
keys.keyPress(java.awt.event.KeyEvent.VK_SPACE )
keys.keyRelease(java.awt.event.KeyEvent.VK_SPACE )
keys.waitForIdle
Amirhosein Ghenaati
Amirhosein Ghenaati 2014년 12월 19일

0 개 추천

i tried to make exe version of key pressing with robot class but my exe file doesn't work and i dont know what i couldnt find relative .jar file for java robot class whats its name ?

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2011년 4월 11일

편집:

dpb
2020년 7월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by