From: Priyanshu Agarwal on
Hi,

I'm using the following command to acquire a character from the keyboard while the focus is on a particular figure.

get(gcf,''CurrentCharacter'')

However, once the character is read the buffer is not cleared and the same character is read even the next time if I use this command in a loop. Is there a way to clear the character from the keyboard buffer?

Thanks.

Regards,
Priyanshu
From: Walter Roberson on
Priyanshu Agarwal wrote:
> Hi,
>
> I'm using the following command to acquire a character from the keyboard
> while the focus is on a particular figure.
> get(gcf,''CurrentCharacter'')
>
> However, once the character is read the buffer is not cleared and the
> same character is read even the next time if I use this command in a
> loop. Is there a way to clear the character from the keyboard buffer?

I don't think so. Well, not officially. As far as the interface is
concerned, the current character remains current until it is changed.
You are intended to use the keypress callback to detect key presses. If
you wanted to, you could have the callback stuff the character away
somewhere, and then when your loop is ready for it, it could see what
the variable currently stored and set the variable to '' afterwards.
From: Matt Fig on
Are you saying that the same character is read even though you are activating the keyboard while the figure is in focus? In other words, why would you expect that the current character would be changed from one reading to the next unless you type while the figure is in focus?

You can set the current character after reading it, though I am still not clear on why you would need to do this.

set(gcf,'currentch',char(1))
From: Priyanshu Agarwal on
What I mean is that the current character should be the character currently pressed on the keyboard. I'm trying to control a robot connected to MATLAB through serial port. So, once I read an arrow key and move the robot based on the key pressed, the key is not flushed from the buffer. This causes the robot to go crazy with the latest command and repeat it until the next key is pressed. However, current character should be empty as soon as the key is read and no other key is pressed.


"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hrvl4a$865$1(a)fred.mathworks.com>...
> Are you saying that the same character is read even though you are activating the keyboard while the figure is in focus? In other words, why would you expect that the current character would be changed from one reading to the next unless you type while the figure is in focus?
>
> You can set the current character after reading it, though I am still not clear on why you would need to do this.
>
> set(gcf,'currentch',char(1))
From: Matt Fig on
So set it after it is read, like I showed above. Use an IF statement, or a SWITCH, so that only the allowed keys make the robot move.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch double(get(gcf,'currentch'))
case 28 % left arrow
% move left
case 29 % right arrow
% move right
case 30 % up arrow
% move up
case 31 % down arrow
% move down
otherwise
end

set(gcf,'currentch','3') % or whatever, as long as it will go to OTHERWISE.
drawnow % Flush the event queue.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%