From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I program keyboard callbacks from anywhere in panel?

I want to put a capability in my LabWindows/CVI application to handle a keyboard sequence (like CTRL-F12) from anywhere in my application (no matter which/how-many panels are open). This sequence will have a callback to automatically turn off the power in case of an emergency.

How do I do it, so the callback can be done from any panel? No matter which panel is running - I load multiple panels into memory upon start of my application.

The new CVI-User.
0 Kudos
Message 1 of 5
(3,089 Views)
Hello

Sounds like you need to setup a systemwide keyboard, which you can do using Win32 functions

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_hooks32.asp

http://www.codeguru.com/system/KBHook.html

In CVI, each panel handles the its own events , so the other thing would be to have a specific callback for each panel that would handle that key combination.

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 5
(3,089 Views)
You can create one callback function that's used for every panel and check for your desired keyboard sequence there. You have to edit each panel to enter the callback function name.
1. In the UI editor, select the main panel.
2. Double-click on a blank spot on the panel to edit the panel (or use the menu to select Edit >> Panel).
3. Enter a function name (e.g. CheckKeypress) in the Callback Function box and click OK.
4. In the UI editor, from the menu select Code >> Set Target File and choose your C file.
5. In the UI editor (with the main panel still selected), from the menu select Code >> Generate >> Panel Callback.
5. In the UI editor (with the main panel still selected), from the menu select Code >> View >> Panel Callback.
6. In the code window, delete case
s in your panel callback which you won't use.
7. Add a new case for EVENT_KEYPRESS:

int keyCode;
switch (event)
{
case EVENT_KEYPRESS:
if ((keyCode = GetKeyPressEventVirtualKey (eventData2)) == 6656)
MessagePopup ("Key Trapped", "Ctrl-F12 was trapped");
break;

Instead of just a MessagePopup, you'll do whatever you want in your shutdown procedure.
6656 is the virtual keycode for Ctrl-F12. The easiest way for me to find virtual keycodes is to set a breakpoint after GetKeyPressEventVirtualKey(), run the program, and see what the return value is (keyCode in this example).
8. Select each of the other panels in your user interface, one by one, edit the panel, and add the name of the callback function you created for the main panel. This way, all panels will use the same callback and can use the same keystroke to call the shutdown procedure. Note that you're just setting the callback function name to be the same for each panel. You have to create only one cal
lback and then tell all panels to use it.
Message 3 of 5
(3,089 Views)
Is there a sample CVI program I can look at? To do this? I will try to adapt the VC6 program into CVI, in the meantime.
0 Kudos
Message 4 of 5
(3,089 Views)
Thank you. Perfect example. I'll try it! 🙂
0 Kudos
Message 5 of 5
(3,089 Views)