Symbian
Symbian OS Library

FAQ-0511 How can I send keypresses to another application?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Application architecture
Created: 07/31/2000 Modified: 06/12/2001
Number: FAQ-0511
Platform: ER5

Question:
How can I send keypresses to another application?

Answer:
This short code sample demonstrates sending the Shift+Ctrl+T keypress to System. On English machines this will have the effect of alternating the "Title position" setting between "Top of columns" and "Left of columns". Note that the caption of 'System' may be different on non-English devices.

void CExampleAppUi::SendKeyTest()
    {
    TKeyEvent event;
    event.iRepeats=0;
    event.iScanCode=0;
    event.iModifiers=EModifierCtrl | EModifierShift;
    event.iCode=CTRL('t'); // defined in COEDEF.H

    _LIT(KAppCaption,"System");
    RWsSession& ws=iEikonEnv->WsSession();
    TInt previous=0;
    CApaWindowGroupName::FindByCaption(KAppCaption,ws,previous);
    TApaTask task(ws);
    task.SetWgId(previous);
    if (!task.Exists())
      User::Leave(KErrNotFound);task.BringToForeground(); // So we can see the results
      task.SendKey(event);
      }

      Other keypress examples could include:

      Ctrl+O:
        event.iModifiers=EModifierCtrl;
        event.iCode=CTRL('o');
        The letter "P":
          event.iModifiers=0;
          event.iCode='P';