MATLAB Answers

command to read all current pressed keyboard keys

조회 수: 174(최근 30일)
Roland Stange
Roland Stange 6 Jul 2017
Answered: Ryan Black 21 Jan 2018
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

Roland Stange
Roland Stange 6 Jul 2017
very nice function! Thanks for that. However, not exactly what i was searching for:-). The function shell not wait for an input, but read currently pressed keys. e.g.: a = whichKeysArePressed()
a =
1×2 cell array
'3' 'k'
for the case that the '3' and the 'k' keys are currently pressed by the user.

로그인 to comment.

답변 수 (4)

Guillaume
Guillaume 6 Jul 2017
Guillaume 님이 편집함. 6 Jul 2017
From base matlab itself, it's not possible. There may be something in the psych toolbox.
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
Walter Roberson 6 Jul 2017
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.

로그인 to comment.


Jan
Jan 6 Jul 2017
Jan 님이 편집함. 6 Jul 2017
You can find such function in the FileExchange. Simply search for the keywords "get pressed kesy".

  댓글 수: 0

로그인 to comment.


Roland Stange
Roland Stange 6 Jul 2017
Thanks very much for your suggenstions. Perfect:-). I will have time next week to test!

  댓글 수: 0

로그인 to comment.


Ryan Black
Ryan Black 21 Jan 2018
I'm about to be dropping a file set into the exchange that contains a key call function to play a virtual music keyboard. You can load different preset sound timbres from an EXCEL Timbre Building Workshop and a MATLAB compiler, or if you know what you are doing, you can build your own timbres in the Timbre Building Workshop! Anyways, if you aren't into that, here is the easy way to get you started on this keycall function:
function [activefig] = keyp()
activefig = figure('KeyPressFcn',@Key_Down,'KeyReleaseFcn',@Key_Up);
end
function [] = Key_Down(src,event)
load workspacein.mat a b c;
user_var=event.Key;
if user_var=='0'
close all
elseif strcmp(user_var,'a')==1
disp(a)
elseif strcmp(user_var,'b')==1
disp(b)
elseif strcmp(user_var,'c')==1
disp(c)
end
end
function [] = Key_Up(~,~)
end
First you will need to save workspacein.mat file into your current folder with variables named a,b,c. These will be the display outputs of your key inputs. Then, run the code and you will be able to press a,b, or c in the activefig and it will instantly display the value without having to press enter. Modify to your desired needs. Also, you can export variables into the base workspace lookup assignin.

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by