Artificial/simulated key press

조회 수: 14 (최근 30일)
Tobias Litherland
Tobias Litherland 2011년 1월 28일
Hi
I'm trying to make a work-around for the problem of not being able to automatically select all text in an edit box when selecting the box. The solution of pressing "ctrl+a" just isn't efficient enough.
So, what I'm wondering is the following: Is there a way for MatLab to simulate a key-press - an artificial key press - so that i can execute a "ctrl+a" on selecting the edit box?
The goal is to make the edit box work as a "normal", select-all-on-press edit box.
If you have informations regarding if this is or isn't possible, please reply. Thanks in advance.
/Tobias

채택된 답변

Tobias Litherland
Tobias Litherland 2011년 1월 28일
Thank you! I followed the link, and and ended up solving it with the following code. This is a callback function for ButtonDownFcn in the edit box. Selects all text on a left-click of the mouse.
%Initialize the java engine
import java.awt.*;
import java.awt.event.*;
%Create a Robot-object to do the key-pressing
rob=Robot;
%Commands for pressing keys:
% If the text cursor isn't in the edit box allready, then it
% needs to be placed there for ctrl+a to select the text.
% Therefore, we make sure the cursor is in the edit box by
% forcing a mouse button press:
rob.mousePress(InputEvent.BUTTON1_MASK );
rob.mouseRelease(InputEvent.BUTTON1_MASK );
% CONTROL + A :
rob.keyPress(KeyEvent.VK_CONTROL)
rob.keyPress(KeyEvent.VK_A)
rob.keyRelease(KeyEvent.VK_A)
rob.keyRelease(KeyEvent.VK_CONTROL)
end
Thanks again!

추가 답변 (1개)

Paulo Silva
Paulo Silva 2011년 1월 28일
http://www.mathworks.com/matlabcentral/newsreader/view_thread/243113

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by