TeamWild
TeamWild

Reputation: 2550

Capture keypress sequence in Windows Mobile device

I need to capture a keypress sequence from a Windows Mobile device to trigger a duress event. If my application was the only thing running I would use a base form event handler to check the keypress BUT... as the application could launch a browser and also uses SOTI, it must also work outside of the main application.

Is it possible to create a TSR application on a Windows Mobile device that can send web service messages (whilst in comms)?

Upvotes: 0

Views: 820

Answers (2)

user1845593
user1845593

Reputation: 1844

I recently needed to get a key code from a handheld device and I just created an empty project with 1 textbox, included the following file and add this bit of code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        HookKeys x = new HookKeys();
        x.Start();
        x.HookEvent += new HookKeys.HookEventHandler(HookEvent);
    }

    private void HookEvent(HookEventArgs e, KeyBoardInfo keyBoardInfo)
    {
        textBox1.Text = "vkCode = " + keyBoardInfo.vkCode + Environment.NewLine + textBox1.Text;
    }
}

Worked for a windows mobile 6.5 professional

Upvotes: 0

josef
josef

Reputation: 5959

I have written several keyboard hook 'applications' that invoke different functions. You can also use that to do socket or webservice calls: http://www.hjgode.de/wp/?s=hook and http://www.hjgode.de/wp/?s=keytoggle

    // The command below tells the OS that this EXE has an export function so we can use the global hook without a DLL
__declspec(dllexport) LRESULT CALLBACK g_LLKeyboardHookCallback(
   int nCode,      // The hook code
   WPARAM wParam,  // The window message (WM_KEYUP, WM_KEYDOWN, etc.)
   LPARAM lParam   // A pointer to a struct with information about the pressed key
)
{
    /*    typedef struct {
    DWORD vkCode;
    DWORD scanCode;
    DWORD flags;
    DWORD time;
    ULONG_PTR dwExtraInfo;
    } KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;*/

    // Get out of hooks ASAP; no modal dialogs or CPU-intensive processes!
    // UI code really should be elsewhere, but this is just a test/prototype app
    // In my limited testing, HC_ACTION is the only value nCode is ever set to in CE
    static int iActOn = HC_ACTION;
    static bool isShifted=false;

#ifdef DEBUG
    static TCHAR str[MAX_PATH];
#endif

    PKBDLLHOOKSTRUCT pkbhData = (PKBDLLHOOKSTRUCT)lParam;
    //DWORD vKey;
    if (nCode == iActOn)
    {
    //only process unflagged keys
    if (pkbhData->flags != 0x00)
        return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam);
    //check vkCode against forbidden key list
    if(pForbiddenKeyList!=NULL)
    {
        BOOL bForbidden=false;
        int j=0;
        do{
            if(pForbiddenKeyList[j]==(BYTE)pkbhData->vkCode)
            {
                bForbidden=true;
                DEBUGMSG(1, (L"suppressing forbidden key: 0x%0x\n",pkbhData->vkCode));
                continue;
            }
            j++;
        }while(!bForbidden && pForbiddenKeyList[j]!=0x00);
        if(bForbidden){
            return true;
        }
    }

    SHORT sShifted = GetAsyncKeyState(VK_SHIFT);
    if((sShifted & 0x800) == 0x800)
        isShifted = true;
    else
        isShifted = false;

    //check and toggle for Shft Key
    //do not process shift key
    if (pkbhData->vkCode == VK_SHIFT){
        DEBUGMSG(1, (L"Ignoring VK_SHIFT\n"));
        return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam);
    }

    //################################################################
    //check if the actual key is a match key including the shift state
    if ((byte)pkbhData->vkCode == (byte)szVKeySeq[iMatched]){
        DEBUGMSG(1 , (L"==== char match\n"));
        if (bCharShiftSeq[iMatched] == isShifted){
            DEBUGMSG(1 , (L"==== shift match\n"));
        }
        else{
            DEBUGMSG(1 , (L"==== shift not match\n"));
        }
    }

    if( wParam == WM_KEYUP ){
        DEBUGMSG(1, (L"---> szVKeySeq[iMatched] = 0x%02x\n", (byte)szVKeySeq[iMatched]));

        if ( ((byte)pkbhData->vkCode == (byte)szVKeySeq[iMatched]) && (isShifted == bCharShiftSeq[iMatched]) ) {

            //the first match?
            if(iMatched==0){
                //start the timer and lit the LED
                LedOn(LEDid,1);
                tID=SetTimer(NULL, 0, matchTimeout, (TIMERPROC)Timer2Proc);
            }
            iMatched++;

            DEBUGMSG(1, (L"iMatched is now=%i\n", iMatched));
            //are all keys matched
            if (iMatched == iKeyCount){
                //show modeless dialog
                DEBUGMSG(1, (L"FULL MATCH, starting ...\n"));
                PostMessage(g_hWnd, WM_SHOWMYDIALOG, 0, 0);
                //reset match pos and stop timer
                DEBUGMSG(1, (L"FULL MATCH: Reset matching\n"));
                LedOn(LEDid,0);
                iMatched=0; //reset match pos
                KillTimer(NULL, tID);
                //return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam);
            }
            //return -1; //do not forward key?
        }
        else
        {
            KillTimer(NULL, tID);
            LedOn(LEDid,0);
            iMatched=0; //reset match pos
            DEBUGMSG(1, (L"FULL MATCH missed. Reseting matching\n"));
        }
    } //if wParam == WM_KEY..
    }
    return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam);
}

Looking for a hook example that looks for a key sequence: http://code.google.com/p/keytoggleboot/source/browse/trunk/KeyToggleBoot/ReadMe.txt?spec=svn14&r=14

There are also articles and posts about keyboard hooks in the internet (ie at codeproject).

Upvotes: 1

Related Questions