Beijer Electronics (formerly QSI Corporation)

Register Events in C#
Page 1 of 1

Author:  NotSoTragicHero [ Wed Dec 16, 2009 1:29 pm ]
Post subject:  Register Events in C#

What is the suggested way to register events or capture key presses on the 1-4 buttons on the TREQ-M4?

After reading through the TREQ-M4 documentation and the C# wrapper documentation, I can't seem to find an even somewhat straightforward way to do this.

Does anybody have a good C# example of this? I've tried a number of ways and have just found dead ends.

Author:  NotSoTragicHero [ Tue Dec 22, 2009 12:01 pm ]
Post subject:  Re: Register Events in C#

Does anyone have any insight in to this at all? Thanks.

Author:  danieln [ Thu Jan 14, 2010 11:02 am ]
Post subject:  Re: Register Events in C#

If you are using the .NET wrappers, you could call the following method defined in TreqKeypadDriver.cs:

TreqKeypadDriver.SetKeypadKey(TreqKeys.TREQ_KEY0, exampleseq1, false);

TreqKeys.TREQ_KEY0 - is an enum also defined in TreqKeypadDriver.cs (where the other key defines exist also)
exampleseq1 - an array of 16-bit unsigned shorts you could use any of the following:
ushort[] exampleseq1 = { 0x70 }; //F1 key
(standard ce virtual key defines come from the SDK, or C:\Program Files\Windows CE Tools\wce600\TREQ-M4\Include\Armv4i\winuser.h starting at line 2956)
false - specifies to map the UNSHIFTED key to your value. For explation on shifted key state see M4 guide section 3.3)

For your sequence you can use other mappings also, for example you could use these:

ushort[] exampleseq2 = { (ushort)TreqVKCodes.VK_SEQ,(ushort)'B' }; //type the LOWERCASE character 'b'
ushort[] exampleseq2  = constructRunAppSeq("\\Windows\\explorer.exe");

The constructRunAppSeq method called is explained here:

        protected static ushort[] constructRunAppSeq(string program)
            ushort[] runApp = new ushort[program.Length + 2];
            runApp[0] = (ushort)TreqVKCodes.VK_RUN_APP; //prepend runapp code
            runApp[program.Length + 1] = 0; //append null terminator
            program.ToCharArray().CopyTo(runApp, 1); //put string in the middle of the two
            return runApp;

Please note that in order to assign a button to type the UPPERCASE character 'B' you would have to create a sequence which will press the virtual shift key, just as you would on a keyboard.

Finally, if you recall the C++ examples on page 50-51 of the M4 userguide, I've created a comparable version example in C#, the following method is a slice of that code:

private void chooseAssignment(int index, TreqKeys key, ref ushort[] keyval, bool shift)
            switch (index)
                case 0: //default
                    switch (key)
                        case TreqKeys.TREQ_KEY0:
                            if (shift) keyval = defaultKey1s; else keyval = defaultKey1; break;
                        case TreqKeys.TREQ_KEY1:
                            if (shift) keyval = defaultKey2s; else keyval = defaultKey2; break;
                        case TreqKeys.TREQ_KEY2:
                            if (shift) keyval = defaultKey3s; else keyval = defaultKey3; break;
                        case TreqKeys.TREQ_KEY3:
                            if (shift) keyval = defaultKey4s; else keyval = defaultKey4; break;
                case 1: //runApp
                    // runApp launches MinTermCE.exe
                    keyval = constructRunAppSeq("\\HardDisk\\bin\\MinTermCE.exe");
                case 2: //runSeq
                    // runSeq launches MinTermCE.exe, then types "done"
                    // Can't use string initializer when data follows the path in
                    // the sequence (compiler limitation)
                    ushort[] runSeq = { (ushort)TreqVKCodes.VK_SEQ | (ushort)TreqVKCodes.TREQ_KEY_REPEAT, (ushort)TreqVKCodes.VK_RUN_APP, '\\', 'H', 'a', 'r', 'd', 'D', 'i', 's', 'k', '\\', 'b', 'i', 'n', '\\', 'M', 'i', 'n', 'T', 'e', 'r', 'm', 'C', 'E', '.', 'e', 'x', 'e', 0, (ushort)'D', (ushort)'O', (ushort)'N', (ushort)'E' | (ushort)TreqVKCodes.TREQ_KEY_RELEASE };
                    keyval = runSeq;
                case 3: //otherSeq
                    ushort[] otherSeq = { (ushort)TreqVKCodes.VK_SEQ | (ushort)TreqVKCodes.TREQ_KEY_REPEAT, (ushort)VKeys.VK_LSHIFT, (ushort)'H' | (ushort)TreqVKCodes.TREQ_KEY_RELEASE, (ushort)'E', (ushort)'L', (ushort)'L', (ushort)'O', (ushort)VKeys.VK_SPACE, (ushort)VKeys.VK_LSHIFT, (ushort)'W' | (ushort)TreqVKCodes.TREQ_KEY_RELEASE, (ushort)'O', (ushort)'R', (ushort)'L', (ushort)'D', (ushort)VKeys.VK_RETURN | (ushort)TreqVKCodes.TREQ_KEY_RELEASE };
                    keyval = otherSeq;

Hopefully this helps.

Author:  Damon8or [ Fri Jan 22, 2010 8:24 am ]
Post subject:  Re: Register Events in C#

If what you want to do is trap the keystrokes of the the keypad keys you'll have to invoke SetWindowsHookEx in coredll.dll. I used the technique in Prathamesh S Kulkarni's Code project article and made my own DLL and P/Invoked it from .NET. If you want to capture only the keypad keys, not from an attached keyboard, you have to examine the scan codes and key values to filter them - see KBDLLHOOKSTRUCT. I passed the window handle to the .NET window as a parameter in my P/Invoke call, and used that in the native side with PostMessage to generate WM_KEYDOWN and WM_KEYUP messages back to that window. You can also control whether to consume or simply get a copy of the keystroke based on calling CallNextHookEx or not. Then I simply handle the keystrokes as you would any keystroke in .NET. Note if your window already has focus, and you copy the keystroke, you'll get duplicate messages.

I noticed there is another approach entirely from .NET by hjgode, but my own attempts at a similar approach were unsuccessful, but I have not tried the code in hjgode's article.

Referenced Code Project Articles. ... -fram.aspx

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group