LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

? Return keycode if key pressed, constant if key not pressed?

My program is doing various other things inside a loop. I want to record a number each time the loop executes. If the user has pressed a key on the keyboard since the last execution of the loop, the number I want to record is the keycode of that key. If the user has not pressed a key, the number I want to record is a constant such as 27.
 
How can I do this? What VI or other construct can I evaluate inside my loop?
 
I've used events and processed keycodes, but I don't see how to substitute my constant if no key has been pressed. Do I want to set the timeout to a very small value so the "timeout" event passes out the constant? Is there something I can test to find out if there is a keycode sitting in a buffer where I can retrieve it?
0 Kudos
Message 1 of 5
(3,120 Views)
One option would be to use keyboardAcquire.vi to get the pressed keys.  It gives an array of all keys pressed.  It will not require an event structure.  If the array is empty, then you know the user didn't press any keys.
0 Kudos
Message 2 of 5
(3,115 Views)
Your suggested method of using the timeout event sounds like the best one to me, although you might have a problem there if you have multiple events and if you need to keep the loop rate constant.

If you want to make sure you catch all keyboard clicks, you can run a seperate loop with an event structure. Whenever a key down event occurs, push the result into a queue.
In your other loop, flush the same queue in each iteration - if there are no elements in the queue, no key was pressed. If there are elements, you can use whatever algorithm you want to decide which of them interests you.

___________________
Try to take over the world!
Message 3 of 5
(3,090 Views)
>run a seperate loop with an event structure. Whenever a key down event occurs, push the result into a queue
 
Thanks!  I am using this approach, but dequeueing one element rather than flushing the queue (which makes more sense because of what I will be doing with them next).
 
I did have one weird difficulty: I don't know how I am supposed to specify the element type will be in the queue when I create that queue with a queue reference. If I follow your suggestion in the most simple way, doing all these things in one vi, the queue reference adapts to the type of element I have wired to the Enqueue Element node. But for other reasons I needed the Enqueue Element node nestled in a subvi and the Create Queue node in the vi that calls it, and apparently the type doesn't propagate upwards through the connector. I worked around this by creating another dummy vi with everything at the same level so that I could cut and paste and relabel the references, and then tossed the dummy, but surely there was a neater way....
0 Kudos
Message 4 of 5
(3,052 Views)
Yes, queue references are strictly typed and it used to annoy me that they would break if you changed the queue data type, until I found out how you can change it - create a control or an indicator for the queue reference, right click it and select Show Control. You can now replace that control with a typedef and it should update all the references when you change it.

___________________
Try to take over the world!
Message 5 of 5
(3,047 Views)