command to read all current pressed keyboard keys
49 ビュー (過去 30 日間)
古いコメントを表示
Hi, i did not find an appropriate answer... is there a simple way without using callbacks to get the current pressed keys on the keyboard.
best, Roland
2 件のコメント
回答 (5 件)
Guillaume
2017 年 7 月 6 日
編集済み: Guillaume
2017 年 7 月 6 日
Otherwise, on Windows, you would have to call the Win32 API GetKeyboardState either through loadlibrary or a mex file. Note that GetKeyboardState will only notice key changes that matlab itself has already seen.
Another option is to use .Net GetKeyStates. .Net is easier to call from matlab, but with this function you'll have to iterate over all the keys to get their status. Something like (completely untested code, I don't have matlab on this computer):
Net.addAssembly('System.Windows.Input');
akey = System.Windows.Input.Key.A; %use any key to get the enum type
keys = System.Enum.GetValues(akey.GetType); %get all members of enumeration
keynames = System.Enum.GetNames(akey.GetType);
keystates = arrayfun(@(key) bitand(System.Windows.Input.Keyboard.GetKeyStates(key), ...
System.Windows.Input.KeyStates.Down), ...
keys);
edit: after testing on computer with matlab, there was a number of bugs with the above, the two major ones being you can't arrayfun a .Net array, and some of the keys may not be valid keys to pass to GetKeyStates. In any case, since we're iterating over the keys, you can use IsKeyDown which has simpler syntax. So, without bugs:
NET.addAssembly('PresentationCore');
akey = System.Windows.Input.Key.A; %use any key to get the enum type
keys = System.Enum.GetValues(akey.GetType); %get all members of enumeration
keynames = cell(System.Enum.GetNames(akey.GetType))';
iskeyvalid = true(keys.Length, 1);
iskeydown = false(keys.Length, 1);
for keyidx = 1:keys.Length
try
iskeydown(keyidx) = System.Windows.Input.Keyboard.IsKeyDown(keys(keyidx));
catch
iskeyvalid(keyidx) = false;
end
end
Once you've run the above once (to get the list of valid keys) you can get the state of the keys with the simpler:
iskeydown(iskeyvalid) = arrayfun(@(keyidx) System.Windows.Input.Keyboard.IsKeyDown(keys(keyidx)), find(iskeyvalid));
1 件のコメント
Walter Roberson
2017 年 7 月 6 日
I do not know the current state of the art, but earlier on it used to be the case that keyboards could typically only reliably distinguish two pressed keys beyond the modifier keys. A contact grid used to be used that had the property that if any three points of a rectangle of keys were pressed then the keyboard could not distinguish which three of the four were pressed.
Keyboards themselves typically encode events rather than key presses: "during this cycle, such-and-such a key was just pressed and this other key was released". The set of keys down at any one time was considered to be the set of keys for which a key-press event had been seen but a key release event had not been seen yet.
Bruno Luong
2021 年 9 月 10 日
This File Exchange works well for me Hebirobotics
No toolbox is required and no need to open a figure() to catch the keypressed callback.
To simplify installation, download and use the compiled version on github.
0 件のコメント
Jan
2017 年 7 月 6 日
編集済み: Jan
2017 年 7 月 6 日
0 件のコメント
balandong
2020 年 3 月 23 日
Can consider this FEX submission KbTimer. It is MEX base thus quite fast
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!