Wie kann ich eine Sendkey anweisung in VC++ realisieren?



  • vielleicht hat jemand ne antwort??

    vielen dank



  • #include <dos.h>
    #include <windows.h>
    #include <windowsx.h>
    #include <winuser.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    // SendKeys.h: Schnittstelle für die Klasse CSendKeys.
    //
    //////////////////////////////////////////////////////////////////////

    #if !defined(AFX_SENDKEYS_H__782042AD_F048_11D6_A418_000374890932__INCLUDED_)
    #define AFX_SENDKEYS_H__782042AD_F048_11D6_A418_000374890932__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    class CSendKeys
    {
    public:
    CSendKeys();
    virtual ~CSendKeys();

    void SendKeys (char *szKTS, char* WindowCaption, bool CaptionExact);

    private:

    long fGetKey (char * szKeysToSend);
    void MWSpecial (int vk);
    void MWClearExt (int * ext);
    void MWSendChar (char cKeyToSend);
    HWND GetWindowByName (char * szWindowName, bool exact);

    typedef struct
    {
    char szKeyName [25];
    int vKey;
    } skeys;

    };

    #endif // !defined(AFX_SENDKEYS_H__782042AD_F048_11D6_A418_000374890932__INCLUDED_)

    ////////////
    ////////////
    ////////////
    ////////////
    ////////////
    ////////////

    #include <dos.h>
    #include <windows.h>
    #include <windowsx.h>
    #include <winuser.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    // SendKeys.cpp: Implementierung der Klasse CSendKeys.
    //
    //////////////////////////////////////////////////////////////////////

    #include "stdafx.h"
    #include "stdafx.h"
    #include "SendKeys.h"

    #ifdef DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE
    _;
    #define new DEBUG_NEW
    #endif

    //////////////////////////////////////////////////////////////////////
    // Konstruktion/Destruktion
    //////////////////////////////////////////////////////////////////////

    CSendKeys::CSendKeys()
    {

    }

    CSendKeys::~CSendKeys()
    {

    }

    void CSendKeys::MWSpecial (int vk)
    {
    keybd_event ((unsigned char)vk, 0, 0, 0);
    keybd_event ((unsigned char)vk, 0, 2, 0);

    }

    long CSendKeys::fGetKey (char * szKeysToSend)
    {

    skeys keys [31]= {
    {"BACKKSPACE", VK_BACK},
    {"BS", VK_BACK},
    {"BKSP", VK_BACK},
    {"DELETE", VK_DELETE},
    {"DEL", VK_DELETE},
    {"DOWN", VK_DOWN},
    {"END", VK_END},
    {"ENTER", VK_RETURN},
    {"ESC", VK_ESCAPE},
    {"HOME", VK_HOME},
    {"INSERT", VK_INSERT},
    {"INS", VK_INSERT},
    {"LEFT", VK_LEFT},
    {"PGDN", VK_NEXT},
    {"PGUP", VK_PRIOR},
    {"RIGHT", VK_RIGHT},
    {"TAB", VK_TAB},
    {"UP", VK_UP},
    {"F1", VK_F1},
    {"F2", VK_F2},
    {"F3", VK_F3},
    {"F4", VK_F4},
    {"F5", VK_F5},
    {"F6", VK_F6},
    {"F7", VK_F7},
    {"F8", VK_F8},
    {"F9", VK_F9},
    {"F10", VK_F10},
    {"F11", VK_F11},
    {"F12", VK_F12}};

    int i;

    for (i=0; i< sizeof (keys) / sizeof (keys [0]); i++)
    if (strncmp (szKeysToSend, keys [i].szKeyName, strlen (szKeysToSend)) == 0)
    return keys [i].vKey;

    return -1;
    }

    void CSendKeys::MWClearExt (int * ext)
    {
    if ((*ext & 0x1) == 0x1)
    {
    keybd_event (VK_SHIFT, 0, 2, 0);
    *ext = (*ext & ! 0x1);
    }
    if ((*ext & 0x2) == 0x2)
    {
    keybd_event (VK_CONTROL, 0, 2, 0);
    *ext = (*ext & ! 0x2);
    }
    if ((*ext & 0x4) == 0x4)
    {
    keybd_event (VK_MENU, 0, 2, 0);
    *ext = (*ext & ! 0x4);
    }

    }

    void CSendKeys::SendKeys (char *szKTS, char* WindowCaption, bool CaptionExact )
    {

    HWND Fenster = GetWindowByName ( WindowCaption, CaptionExact );

    if ( Fenster == NULL )
    return;

    BringWindowToTop(Fenster);
    SetActiveWindow(Fenster);

    int l;
    int t = 0;
    char szHelp[255];
    char szKeysToSend[25];
    bool clearExt;
    int ext = 0;

    int o;
    int o2;

    int n;

    int i;

    wsprintf (szKeysToSend, "%s", szKTS);
    l = strlen (szKeysToSend);

    while (t <= l -1)
    {
    clearExt = false;

    switch (szKeysToSend [t])
    {
    case '{': // Sondertasten
    o = strchr (szKeysToSend + t, '}') - szKeysToSend;
    if (o == 0)
    break;

    strncpy (szHelp, szKeysToSend + t + 1, o - t - 1);

    szHelp [o-t-1] = '\0';

    strupr (szHelp);

    n = 1;
    o2 = strchr (szHelp, ' ') - szHelp ;

    if (o2 > 0)
    {
    n = atoi (szHelp + o2 + 1);
    szHelp [o2] = '\0';

    }

    for (i=0;i< n; i++)

    switch (szHelp[0])
    {
    case '+':
    case '^':
    case '%':
    case '{':
    case '}':
    case '(':
    case ')':
    case '~':
    MWSendChar (szHelp [0]);
    break;
    default:

    MWSpecial (fGetKey (szHelp));

    t = o +1 ;
    break;

    }

    break;

    case '+': // Umschalt / SHIFT
    keybd_event (VK_SHIFT, 0, 0, 0);
    ext = 0x1;
    t += 1;
    break;

    case '^': // STRG / CTRL
    keybd_event (VK_CONTROL, 0, 0, 0);
    ext = 0x2;
    t += 1;
    break;

    case '%': // ALT
    keybd_event (VK_MENU, 0, 0, 0);
    ext = 0x4;
    t += 1;
    break;

    case '~': // Enter
    MWSpecial (VK_RETURN);
    t += 1;
    break;

    case '(': // Zusammenfassung
    o = strchr (szKeysToSend, ')') - szKeysToSend;
    break;

    default:
    MWSendChar (szKeysToSend [t]);
    t += 1;
    clearExt = true;
    break;

    }

    if (ext && clearExt)
    MWClearExt (&ext);

    }
    if (ext)
    MWClearExt (&ext);
    }

    void CSendKeys::MWSendChar (char cKeyToSend)
    {

    int vko;
    int vk;
    int shift;

    vko = VkKeyScan(cKeyToSend);
    vk = (vko & 0xFF);

    shift = (vko & 0x100) == 0x100;

    if (shift)
    keybd_event (VK_SHIFT, 0, 0, 0);

    keybd_event ((unsigned char)vk, 0, 0, 0); // Taste drücken WM_KEYDOWN
    keybd_event ((unsigned char)vk, 0, 2, 0); // Taste wieder loslassen WM_KEYUP

    if (shift)
    keybd_event (VK_SHIFT, 0, 2, 0);

    }

    HWND CSendKeys::GetWindowByName (char * szWindowName, bool exact )
    {

    HWND hWindow = NULL;
    char * szFenstername;

    if (exact)
    return FindWindow (NULL, szWindowName);
    else
    {

    hWindow = FindWindow (NULL, NULL);
    hWindow = GetWindow (hWindow, GW_HWNDFIRST);

    while (hWindow != NULL)
    {
    UINT textLength=GetWindowTextLength(hWindow);

    if(textLength)
    {

    szFenstername = new char [textLength +2];
    GetWindowText(hWindow,szFenstername,textLength+1);
    CString Fenstertext(szFenstername);

    delete[] szFenstername;
    if ( Fenstertext.Find(szWindowName,0) == 0 )
    return hWindow;
    }

    hWindow = GetWindow (hWindow, GW_HWNDNEXT);

    }
    }
    return NULL;
    }

    //------------------------------------------------------------------------------
    //------------------------------------------------------------------------------
    //------------------------------------------------------------------------------



  • ^schön aber mit CodeTags wäre das ganze auch lesbar...

    Devil



  • Weiterhin sind wir hier im MFC-Forum!
    Solche Posts wie die von Sunny sind einfach nur Chaos! Sie haben
    1. nix mit MFC zu tun
    2. werden ANSI-C und C++ vermischt, man weiss also nicht einmal, ob es nun eher nach ANSI-C oder nach C++ gehört!



  • Hol dir ein Handle oder sowas auf das Fenster das deinen "Key" "gesendet" kriegen will 😃 😃 😃
    Dann kannst du mit SendMessage ne WM_KEYDOWN Nachricht senden.


Anmelden zum Antworten