LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Emulate Keyboard strokes

Hi,
 
I was wondering if it was possible to emulate keyboard strokes through LabWindows. I would like to create a gui with various buttons that interacts with a CNC controller software called Mach 3. Due to limitations of inputs through a serial port, if you want to add a lot of external inputs, you have to buy a keyboard emulator like I-Pac. I would like to skip that route, and just have a small touch screen monitor with my custom software. I have a fair amount of experience with LabWindows, but have never had to try to make such a project work. Any help will be greatly appreciated.
 
Thanks
 
Sebastian
0 Kudos
Message 1 of 9
(6,076 Views)
If you want to emulate keyboard strokes you can use FakeKeystroke () function: the keyboard events are received by the active panel.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 9
(6,068 Views)

Hi,

Thanks for the reply, but I have another question. I found the Fakekeystroke command, but is it possible to use it to send a keystroke to another program that is running in processes and not the current labwindows gui window that is shown?

Thanks

Sebastian

0 Kudos
Message 3 of 9
(6,036 Views)
Sebastian,

You cannot use FakeKeystroke to send a keystroke to another app, but you can use the Windows SDK to do this. (If you haven't done it before, to use the Windows SDK in CVI, just add a #include "windows.h" statement to the top of your program).

The SDK functions that you should look into are:

    EnumWindows - to enumerate all the windows in the system.
    GetWindowText - to obtain the title of each window. I'm assuming you can find the application that you want to send the key to, by the name of its main window.
    PostMessage - to send the fake keystroke to the application's window. You will probably need to send both a WM_KEYDOWN and a WM_KEYUP message, but you might hve to experiment with this to see how the application responds.

If you're not already familiar with these functions, or with the SDK in general, you should go to msdn.microsoft.com and search for these functions to get a detailed description of how to use each of them.

Hope this helps.

Luis
Message 4 of 9
(6,005 Views)

Hi Luis,

Thank you very much. I will look into these functions and will see if it will be possible to do. I will post questions if any arise.

Thanks

Sebastian

0 Kudos
Message 5 of 9
(5,970 Views)
Hi,
 
Can I do the following:
 
iHwnd = FindWindow(NULL, "Mach 3");
test = PostMessage(iHwnd, 41, NULL, NULL);
 
I saw this example that was used to close an application and I can't get it to work for me. test!=0 for the return value so it should work, but nothing happens. Any help will be greatly appreciated.
 
Thanks
 
Sebastian
0 Kudos
Message 6 of 9
(5,948 Views)
You can certainly try that scheme, but what were you expecting

test = PostMessage(iHwnd, 41, NULL, NULL);

to do? 41 isn't a standard Windows message number, as far as I am aware. Certainly not a published one. Try looking at the documentation for the WM_KEYDOWN, WM_KEYUP and WM_CHAR messages.
--
Martin
Certified CVI Developer
0 Kudos
Message 7 of 9
(5,938 Views)
Hi,
 
I thought that if I entered the ascii number than that character would be sent. I am going to look into those commands tomorrow, but does anyone know the Windows message for regular keyboard strokes like the letter 'a' or 'j' for example?
 
Thanks
 
Sebastian
0 Kudos
Message 8 of 9
(5,925 Views)
Sebastian,

The problem in your code is that you're passing the ASCII code in the parameter that is reserved for the message. In that parameter you have to pass a message that represents the keystroke, such as WM_KEYDOWN. You then pass the key specifier in one of the other parameters, and this is where the ASCII code comes into play.

You should go to msdn.microsoft.com and look up WM_KEYDOWN for a detailed description of this message and its parameters.

Luis
0 Kudos
Message 9 of 9
(5,912 Views)