while trying to automate keyPress event in a loop, I get the error: "Unrecognized function or variable 'VK_i'

23 次查看(过去 30 天)
I am trying to automate/simualte the keyboard to automatically generate/type numbers using the java robot class and I running into the following error: Unrecognized function or variable 'VK_i'. Could someone help. Here is my code snippet:
clc; clear; close all;
import java.awt.Robot;
import java.awt.event.*;
import java.awt.event.KeyEvent;
keybrd = java.awt.Robot; %Create a keyboard object
for i = 1:3
%num = num2str(i);
key = java.awt.event.KeyEvent.VK_i;
keybrd.keyPress(key);
keybrd.keyRelease(key);
end

采纳的回答

Voss
Voss 2022-8-8
key = java.awt.event.KeyEvent.(sprintf('VK_%d',i));
  2 个评论
Syed Bahrez Nadeem
Thanks! That worked. In a second for loop of the same code, I am trying to generate/type letters and I am running into indexing errors. Could you please help with this?
import java.awt.Robot;
import java.awt.event.*;
import java.awt.event.KeyEvent;
keybrd = java.awt.Robot; %Create a keyboard object
%Iterate over the length of ext. Generate and press a letter key
ext = 'tf';
for i = 1:length(ext)
% inp = strcat('java.awt.event.KeyEvent.VK_',ext(i)); %Ex: 'VK_t'
% key = eval(inp); %Evaluates the concatenated string inp and returns it
%index = num2str(i);
key = java.awt.event.KeyEvent.(sprintf('VK_%d',ext(num2str(i))));
keybrd.keyPress(key);
keybrd.keyRelease(key);
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by