Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

simulating keystroke - print a string.

#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

void output ( string string1 );

int main ()
{
      Sleep ( 5000 );
      
      // input hello hoshen
      
      string string1;
      string1 = "hellohEEpeople";
      output ( string1 );
      
      
      return 0;
}



void output ( string string1 )
{
      int size = string1.size();

      for (int i = 0; i < size; ++i)
      {
         BYTE vk = LOBYTE(::MapVirtualKey(string1[i], 0));

         keybd_event(string1[i],vk,0,0);
         keybd_event(string1[i],vk,KEYEVENTF_KEYUP,0);
      }
     
}





this actually would print gibbrish, how can it be fixed?
Avatar of Ichijo
Ichijo

The first argument to keybd_event is supposed to be a virtual-key code, not an ASCII character.

SendInput() lets you supply Unicode character values, so I used it instead.
void output( const wstring& string1 )
 
int main ()
{
      Sleep ( 5000 );
      
      // input hello hoshen
      
      wstring string1;
      string1 = L"hellohEEpeople";
      output ( string1 );
      
      
      return 0;
}
 
 
 
void output( const wstring& string1 )
{
    for( wstring::const_iterator it = string1.begin(); it != string1.end(); ++it )
    {
        INPUT in;
        ZeroMemory( &in, sizeof(in) );
 
        in.type = INPUT_KEYBOARD;
        in.ki.wScan = *it;
        in.ki.dwFlags = KEYEVENTF_UNICODE;
        SendInput( 1, &in, sizeof(in) );
 
        in.ki.dwFlags |= KEYEVENTF_KEYUP;
        SendInput( 1, &in, sizeof(in) );
    }
}

Open in new window

Avatar of Troudeloup

ASKER

what if I need keys like enter and F1?
actually, i think I prefer keybd_event()  if it's ok.


can you correc the codes in the original post for me?

#include <vector>
 
using namespace std;
 
struct KEYINPUT
{
    BYTE virtualKey;
    DWORD nFlags;
};
 
typedef vector<KEYINPUT> VKVECTOR;
void output ( const VKVECTOR& virtualKeys )
{
    for( VKVECTOR::const_iterator it = virtualKeys.begin(); it != virtualKeys.end(); ++it )
    {
        BYTE vsc = LOBYTE(::MapVirtualKey(it->virtualKey, 0));
        keybd_event(it->virtualKey,vsc,it->nFlags,0);
    }
}
 
int main ()
{
    Sleep ( 5000 );
 
    KEYINPUT ki[12] = {
        VK_SHIFT, 0,
        0x48, 0, // H
        0x48, KEYEVENTF_KEYUP,
        VK_SHIFT, KEYEVENTF_KEYUP,
        0x45, 0, // e
        0x45, KEYEVENTF_KEYUP,
        0x4C, 0, // l
        0x4C, KEYEVENTF_KEYUP,
        0x4C, 0, // l
        0x4C, KEYEVENTF_KEYUP,
        0x4F, 0, // o
        0x4F, KEYEVENTF_KEYUP
    };
 
    VKVECTOR vInput;
    for(int i = 0; i < 12; ++i)
    {
        vInput.push_back(ki[i]);
    }
 
    output( vInput );
 
    return 0;
}

Open in new window

BTW, a list of virtual key codes (such as 0x48 for H in my code above) is at: http://msdn2.microsoft.com/en-us/library/ms645540.aspx
Avatar of jkr
Just a small thing missing:

#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

void output ( string string1 );

int main ()
{
      Sleep ( 5000 );
     
      // input hello hoshen
     
      string string1;
      string1 = "hellohEEpeople";
      output ( string1 );
     
     
      return 0;
}



void output ( string string1 )
{
      int size = string1.size();

      for (int i = 0; i < size; ++i)
      {
         BYTE vk = LOBYTE(::MapVirtualKey(string1[i], 0));

         keybd_event(VkKeyScan(string1[i]),vk,0,0);
         Sleep(100);
         keybd_event(VkKeyScan(string1[i]),vk,KEYEVENTF_KEYUP,0);
      }
     
}

'VkKeyScan()' is needed for the shift state.
how about special keys like F1 and enter?
For that you'd use 'VK_F1' and 'VK_RETURN', e.g.

      string1 = "helloh EEpeople";
      string1[6] = VK_RETURN;

At least, that works for mne here. For a more in-depth article about this issue, read http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp ("SendKeys in C++")
string1[6] = VK_RETURN;


what is this?



also, what is mne?
#include <iostream>

using namespace std;

int main ()
{
      string string1 = "hellohoshen";
      string1[6] = VK_RETURN;
      
      cout << string1 << endl;
      
      return 0;
      
}

test.cpp:8: error: `VK_RETURN' was not declared in this scope
You forgot

#include <windows.h>
>>also, what is mne?

'me' - seems to be my typo day ;o)
O_o



why not use sendkeys in the first place?
oh nvm, it's script.


but then again this doesn't capitalize EE and it doesn't show decimal, am I using special keys correcty?


#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

void output ( string string1 );

int main ()
{
      Sleep ( 5000 );
     
      // input hello hoshen
     
      string string1;
      string1 = "hellohEEpeople";
      string1[14] = VK_DECIMAL;
      Sleep ( 3000 );


      output ( string1 );
     
     
      return 0;
}






void output ( string string1 )
{
      int size = string1.size();

      for (int i = 0; i < size; ++i)
      {
         BYTE vk = LOBYTE(::MapVirtualKey(string1[i], 0));

         keybd_event(VkKeyScan(string1[i]),vk,0,0);
         Sleep(100);
         keybd_event(VkKeyScan(string1[i]),vk,KEYEVENTF_KEYUP,0);
      }
     
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial