Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 97
  • Last Modified:

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?
0
Troudeloup
Asked:
Troudeloup
  • 7
  • 5
  • 3
1 Solution
 
IchijoCommented:
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

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


can you correc the codes in the original post for me?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
IchijoCommented:

#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

0
 
IchijoCommented:
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
0
 
jkrCommented:
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.
0
 
TroudeloupAuthor Commented:
how about special keys like F1 and enter?
0
 
jkrCommented:
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++")
0
 
TroudeloupAuthor Commented:
string1[6] = VK_RETURN;


what is this?



also, what is mne?
0
 
TroudeloupAuthor Commented:
#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
0
 
jkrCommented:
You forgot

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

'me' - seems to be my typo day ;o)
0
 
TroudeloupAuthor Commented:
O_o



why not use sendkeys in the first place?
0
 
TroudeloupAuthor Commented:
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);
      }
     
}
0
 
jkrCommented:
Well, I pointed you to that article several times already ;o)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now