Asynchronous Input from PS/2 keyboard

Hi, I have a question on asynchronous input from keyboard (not instrument).
I am trying to write a chess program using the parallel computing toolbox with nVidia GPU.
In MATLAB, I can write a code like
move = input('Input your move:', 's');
However, during waiting opponent's move, MATLAB can't calculate anything until opponent's move is specified. I want to simulate possible moves while waiting until an opponent's move.
http://www.mathworks.com/help/techdoc/matlab_external/f62852.html This is related article from MathWorks, but it is for instruments. Is there any possible way that I can calculate possible moves in background while waiting input?
Thank you for your help.

 Accepted Answer

If I recall correctly, there is a MATLAB File Exchange (FEX) contribution that will read a key if one is waiting and will otherwise continue on running.
The supported way of working is to have a figure and set its KeyPressFcn callback. In order for the callback to be serviced, the thinking routine must use pause() or drawnow() or one of a few other routines that check the event queue.

4 Comments

Hi, Walter.
Thank you for your help. I am still not sure how it works, however.
set(gcf, 'KeyPressFcn', {@LookAtKey}, 'UserData', []);
function LookAtKey(src, event)
%check to see if the key should be ignored. If not, then
set(src, 'UserData', event);
end
function keyevent = think(figno)
while true
drawnow();
keyevent = get(figno, 'UserData');
if ~isempty(keyevent); break; end
%calculate a few more moves
end
end
Then you would have some kind of loop reacting to the user, drawing the board, whatever. When there was spare time, you would call think(), passing in your figure number. When think returns, the return value would be a KeyPressFcn Event Structure such as is documented in http://www.mathworks.com/help/techdoc/ref/figure_props.html
Oh yes: after you have processed the key, be sure to reset the figure UserData to [] . And watch out for several keys in a row...
wow... this is so interesting!
This worked. I wanted the same functionality without any graph, but this is so awesome, so I will go with this.
Thank you very much, Walter.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!