ginput doesn't accept keyboard entry as first input

5 views (last 30 days)
Hello all,
I have a figure in which I use ginput() to have coordinates. I ask the user to use left click, right click or keyboard's space in order to have coordinates of three specifics points. But ginput doesn't respond to any keyboard input till I click on the axes. After the first click it will accept all keyboard inputs.
My code:
[clicX, clicY, clicButton] = ginput(1);
while isempty(clicButton) ~= 1 % ENTER to quit
switch clicButton
case 1 % Left click
...
case 2 % Right click
...
case 32 % SPACE key
...
end
[clicX, clicY, clicButton] = ginput(1);
end
I try to add figure(handle) before ginput() in order to Matlab to focus, but whithout any results.
Is someone have a solution ?
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 12 Aug 2015
dbtype ginput
At around line 87 does it have a comment that begins with g467403 ? If so then I suspect that section to be the difficulty, that possibly before the click the wrong figure is the first child.
Possibly what might work is
uistack(handle, 'top')

More Answers (1)

Denis LE BRET
Denis LE BRET on 7 Jul 2016
Hello,
I have the same problem, the first ginput can't be interrupted with keyboard. Your solution don't work for me, maybe I use it the wrong way. Is "handle" the handle of the figure or the handle of the axes ? I used both of them with no result. My code is attached to this answer if you please can have a look. Thank you.
  4 Comments
Walter Roberson
Walter Roberson on 8 Jul 2016
Please describe the circumstances in more detail. How many figures exist at the time you issue the ginput() ? Is it possible that the one you wish to ginput on is not the top figure? Which operating system are you using and which operating system release?
For example I just started R2012a (I had it handy) in OS-X El Capitan and with no existing figure at all (so impossible that any figure had focus) gave the command
[clicX, clicY, clicButton] = ginput(1);
and without clicking anywhere in the figure, typed a character. The ginput worked and clickButton had exactly the data that would be expected (that is, char(clickButton) was exactly what I had typed.)
... I just installed R2013a on the system described above and the results were exactly the same.
Denis LE BRET
Denis LE BRET on 11 Jul 2016
Hello Walter,
I confirm your example works perfectly on my configuration. the function code is attached with my first message (the uistack call is commented but I tried uncommented and the result was the same). The main difference is that the ginput function is called by a button. You are certainly right, there is a focus problem but I don't manage to solve it. Maybe because I don't use the right handle?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!