Virtual Key Events

This is a discussion on Virtual Key Events within the Windows Programming forums, part of the Platform Specific Boards category; Alright I and my associate are attempting to create a small 3rd party aplication that causes another program to think ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Virtual Key Events

    Alright I and my associate are attempting to create a small 3rd party aplication that causes another program to think you are pressing the arrow keys. This program's purpose is to briing a First Person Shooter movement system (Using the W,A,S, and D keys) into a Real Time Stratagy game (God-View).

    Below is the code thus-far. In-game it works fine, the program registers that WAS or D has been hit, but it does not seem to virtually hit the arrow keys. The game is called 'Warcraft III - The Frozen Throne' which requries DirectX8.1

    Any help would be apreciated (Even if it has nothing to do with fixing this bug, if you see any other bugs, please inform me.)

    Code:
    //---------------------------------------------------------------------------
     
    #include <vcl.h>
    #pragma hdrstop
     
    #include "Unit1.h"
    #include "Unit2.h"
    #include "windows.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma link "trayicon"
    #pragma resource "*.dfm"
    TForm1 *Form1;
    int active = 0;
    HDC mutex;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
    	//aby program byl spustenej jen jednou
    	SetLastError(0);
    	mutex=CreateMutex(NULL,0,"FPSInjector-Jirka");
    	if(GetLastError()==ERROR_ALREADY_EXISTS)
    	{
    		ShowMessage("FPS:Injector is already running.");
    		Application->Terminate();
    		// ExitProces(); 
    	}
    //------------------------------------
     
    	active =0;
    	btnButton1->BringToFront();
    	btnButton2->SendToBack();
     
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::tmrKeyboardTimer(TObject *Sender)
    {
    	if (active ==0)
    	{
    		return;
    	}
    	/*
    	w=119
    	W=87
    	*/
    	if(GetAsyncKeyState(119))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'w' pressed ==> Arrow up");
    		keybd_event(VK_UP,MapVirtualKey(VK_UP,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_UP,MapVirtualKey(VK_UP,0),KEYEVENTF_KEYUP,0);
    	}
    	if(GetAsyncKeyState(87))
    	{
    	Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'W' pressed ==> Arrow up");
    	keybd_event(VK_UP,MapVirtualKey(VK_UP,0),0,0);
    	//keybd_event('C',MapVirtualKey('C',0),0,0);
    	//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    	keybd_event(VK_UP,MapVirtualKey(VK_UP,0),KEYEVENTF_KEYUP,0);
    	}
    	/*
    	A=65
    	a=97
    	*/
    	if(GetAsyncKeyState(65))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'A' pressed ==> Arrow left");
    		keybd_event(VK_LEFT,MapVirtualKey(VK_LEFT,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_LEFT,MapVirtualKey(VK_LEFT,0),KEYEVENTF_KEYUP,0);
    	}
    	if(GetAsyncKeyState(97))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'a' pressed ==> Arrow left");
    		keybd_event(VK_LEFT,MapVirtualKey(VK_LEFT,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_LEFT,MapVirtualKey(VK_LEFT,0),KEYEVENTF_KEYUP,0);
    	}
    	/*
    	S=83
    	s=115
    	*/
    	if(GetAsyncKeyState(83))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'S' pressed ==> Arrow down");
    		keybd_event(VK_DOWN,MapVirtualKey(VK_DOWN,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_DOWN,MapVirtualKey(VK_DOWN,0),KEYEVENTF_KEYUP,0);
    	}
    	if(GetAsyncKeyState(115))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 's' pressed ==> Arrow down");
    		keybd_event(VK_DOWN,MapVirtualKey(VK_DOWN,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_DOWN,MapVirtualKey(VK_DOWN,0),KEYEVENTF_KEYUP,0);
    	}
    	/*
    	D=68
    	d=100
    	*/
    	if(GetAsyncKeyState(68))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'D' pressed ==> Arrow right");
    		keybd_event(VK_RIGHT,MapVirtualKey(VK_RIGHT,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_RIGHT,MapVirtualKey(VK_RIGHT,0),KEYEVENTF_KEYUP,0);
    	}
    	if(GetAsyncKeyState(115))
    	{
    		Form2->mmoOutput->Lines->Add(TimeToStr(Time()) + " 'd' pressed ==> Arrow right");
    		keybd_event(VK_RIGHT,MapVirtualKey(VK_RIGHT,0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),0,0);
    		//keybd_event('C',MapVirtualKey('C',0),KEYEVENTF_KEYUP,0);
    		keybd_event(VK_RIGHT,MapVirtualKey(VK_RIGHT,0),KEYEVENTF_KEYUP,0);
    	}
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::imgBackgroundDblClick(TObject *Sender)
    {
    	Form2->Show();
     
    	Form2->Width = Form1->Width;
     
    	if(TrayIcon->Visible==true)
    	{
    		TrayIcon->Visible=false;
    	}
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnButton2Click(TObject *Sender)
    {
    	btnButton2->SendToBack();
    	btnButton1->BringToFront();
    	active=0;
    	//tmrKeyboard->Enabled = false;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Image1Click(TObject *Sender)
    {
    	ShellExecute(Application->Handle,"open","http://www.Clan-OutlawZ.info",NULL, NULL, SW_SHOWDEFAULT);
     
    }
    //---------------------------------------------------------------------------
     
     
     
    void __fastcall TForm1::WndProc(Messages::TMessage &Message)
    {
    	switch(Message.Msg)
    	{
    		case WM_SYSCOMMAND:
    		if(Message.WParam==SC_MINIMIZE)
    		{
    			TrayIcon->Visible=true;
    		}
    		break;
     
    	}
    	TForm::WndProc(Message);
    }
     
    //---------------------------------------------------------------------------
     
     
    void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
    	CloseHandle(mutex); 
    }
    //---------------------------------------------------------------------------
     
     
     
    void __fastcall TForm1::TrayIconRestore(TObject *Sender)
    {
    	TrayIcon->Visible=false; 
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm1::mnuExitClick(TObject *Sender)
    {
    	Application->Terminate(); 
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm1::mnuActivateClick(TObject *Sender)
    {
    	if(mnuActivate->Caption=="A&ctivate")
    	{
    		//aktivace
    		btnButton1Click(NULL);
    		mnuActivate->Caption="&Deactivate";
    		return;
    	}
    	//deakticvace
    	btnButton2Click(NULL);
    	mnuActivate->Caption="A&ctivate";
     
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm1::mnuTheOutlawZClick(TObject *Sender)
    {
    	Image1Click(NULL); 
    }
    //---------------------------------------------------------------------------
     
     
     
    void __fastcall TForm1::btnButton1Click(TObject *Sender)
    {
    	btnButton1->SendToBack();
    	btnButton2->BringToFront();
    	active=1;
    	//tmrKeyboard->Enabled = true; 
    }
    //---------------------------------------------------------------------------
     
     
    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
    	Form2->Top = Form1->Top + Form1->Height;
    	Form2->Left = Form1->Left;
    }
    //---------------------------------------------------------------------------
    Last edited by sean; 09-07-2005 at 06:39 PM. Reason: Indentation

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm guessing the game is using direct input. That being the case, it would interface directly with the hardware (keyboard) (see here). According to msdn:
    The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message
    Direct input doesn't rely on windows messages, so that might be why using keybd_event doesn't work. Also, msdn says to use SendInput for 2000/NT/XP, although I'm not sure if using that would fix the problem...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Sorry to revive a pretty much dead thread, but finally I got SendInput to work, however it does not fix the problem. Any other ideas?

    Note: Warcraft III Requires DirectX, does that perhaps explain the issue?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Bump.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Closed.

    Please refer to the Forum Guidelines:

    http://cboard.cprogramming.com/annou...ouncementid=51

    Bumping is against forum rules. Please also refrain from contacting moderators directly if your only purpose is to report a lack of activity in your thread. There is nothing we can or will do to stimulate interest in your problems on the internet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21