Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Timed SendKey
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Tue Dec 23, 2008 4:01 pm    Post subject: [C#] Timed SendKey Reply with quote

How do i send a key with a time interval of 1000 ms to an unfocused application?

Examples would do great.

Thanks
Back to top
View user's profile Send private message MSN Messenger
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Tue Dec 23, 2008 4:30 pm    Post subject: Reply with quote

Interval of 1 Second:

Thread.Sleep(1000);

To an unfocused window:

Import and use PostMessage/SendMessage API.

_________________
Back to top
View user's profile Send private message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Tue Dec 23, 2008 5:58 pm    Post subject: Reply with quote

Here it goes:

Code:
public const Int32 VK_RCONTROL = 0xA3;
public const Int32 VK_LEFT = 0x25;
public const Int32 VK_UP = 0x26;
public const Int32 VK_RIGHT = 0x27;
public const Int32 VK_DOWN = 0x28;


if (checkBox7.Checked == true)
            {
                SendMessage(this.Handle, VK_RCONTROL, VK_LEFT, 0);
                SendMessage(this.Handle, VK_RCONTROL, VK_UP, 0);
                SendMessage(this.Handle, VK_RCONTROL, VK_RIGHT, 0);
                SendMessage(this.Handle, VK_RCONTROL, VK_DOWN, 0);
                Thread.Sleep(1000);
            }


Code:

C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(291,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, System.IntPtr, System.IntPtr)' has some invalid arguments
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(291,55): error CS1503: Argument '3': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(291,64): error CS1503: Argument '4': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(292,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, System.IntPtr, System.IntPtr)' has some invalid arguments
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(292,55): error CS1503: Argument '3': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(292,62): error CS1503: Argument '4': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(293,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, System.IntPtr, System.IntPtr)' has some invalid arguments
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(293,55): error CS1503: Argument '3': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(293,65): error CS1503: Argument '4': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(294,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, System.IntPtr, System.IntPtr)' has some invalid arguments
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(294,55): error CS1503: Argument '3': cannot convert from 'int' to 'System.IntPtr'
C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs(294,64): error CS1503: Argument '4': cannot convert from 'int' to 'System.IntPtr'



Just tell me what i did wrong Rolling Eyes Embarassed
Back to top
View user's profile Send private message MSN Messenger
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Tue Dec 23, 2008 6:05 pm    Post subject: Reply with quote

You want to send WM_KEYDOWN.

And how did you define your import for SendMessage (I recommend PostMessage cause you don't really need the return).

_________________
Back to top
View user's profile Send private message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Tue Dec 23, 2008 6:20 pm    Post subject: Reply with quote

Code:
//C# Signature for the SendMessage() API
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);


So what should i do if i want to send CTRL + Left Arrow?
Back to top
View user's profile Send private message MSN Messenger
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Tue Dec 23, 2008 9:53 pm    Post subject: Reply with quote

Code:

public const IntPtr VK_RCONTROL = 0xA3;
public const IntPtr VK_LEFT = 0x25;
public const IntPtr VK_UP = 0x26;
public const IntPtr VK_RIGHT = 0x27;
public const IntPtr VK_DOWN = 0x28;

SendMessage(this.Handle, VK_RCONTROL, VK_LEFT, IntPtr.Zero);
SendMessage(this.Handle, VK_RCONTROL, VK_UP, IntPtr.Zero);
SendMessage(this.Handle, VK_RCONTROL, VK_RIGHT, IntPtr.Zero);
SendMessage(this.Handle, VK_RCONTROL, VK_DOWN, IntPtr.Zero);

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Wed Dec 24, 2008 3:17 am    Post subject: Reply with quote

I got
Code:
The type 'System.IntPtr' cannot be declared const   C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs
The type 'System.IntPtr' cannot be declared const   C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs
The type 'System.IntPtr' cannot be declared const   C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs
The type 'System.IntPtr' cannot be declared const   C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs
The type 'System.IntPtr' cannot be declared const   C:\Documents and Settings\A\Skrivbord\C#\Trainer\Trainer\Form1.cs
Back to top
View user's profile Send private message MSN Messenger
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Wed Dec 24, 2008 3:41 am    Post subject: Reply with quote

the error says that you can't set IntPtr as const try replace them with int
then at the import line switch wParam & lParam to long / int whatever i guess it could be it
Back to top
View user's profile Send private message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Wed Dec 24, 2008 3:49 am    Post subject: Reply with quote

I did

Code:


        public const int VK_RCONTROL = 0xA3;
        public const int VK_LEFT = 0x25;
        public const int VK_UP = 0x26;
        public const int VK_RIGHT = 0x27;
        public const int VK_DOWN = 0x28;

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, long wParam, int lParam);


And the result is:

Code:
(292,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, long, int)' has some invalid arguments
(292,64): error CS1503: Argument '4': cannot convert from 'System.IntPtr' to 'int'
(293,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, long, int)' has some invalid arguments
(293,62): error CS1503: Argument '4': cannot convert from 'System.IntPtr' to 'int'
(294,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, long, int)' has some invalid arguments
(294,65): error CS1503: Argument '4': cannot convert from 'System.IntPtr' to 'int'
(295,17): error CS1502: The best overloaded method match for 'tibian.Form1.SendMessage(System.IntPtr, int, long, int)' has some invalid arguments
(295,64): error CS1503: Argument '4': cannot convert from 'System.IntPtr' to 'int'
Back to top
View user's profile Send private message MSN Messenger
sloppy
Expert Cheater
Reputation: 0

Joined: 17 Aug 2008
Posts: 123

PostPosted: Wed Dec 24, 2008 8:36 am    Post subject: Reply with quote

^ You are passing an IntPtr as the 4th argument.. but the function is expecting an int, update the definition.

SendMessage() will not return until the message has been processed, consider using PostMessage(). Instead of defining the virtual key codes for each key, try using the System.Windows.Forms.Keys enum.

Code:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

const uint WM_KEYDOWN = 0x100;

Code:
PostMessage(hWnd, WM_KEYDOWN, (int)Keys.A, 0);
Back to top
View user's profile Send private message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Thu Dec 25, 2008 12:07 pm    Post subject: Reply with quote

Code:
PostMessage(hWnd, WM_KEYDOWN, (int)Keys.A, 0);

Was great!

But the thing i dont get is: How do i tell it to hold down CTRL while pressing a key?

I searched on Msdn but couldn't fint out how to do that.
Back to top
View user's profile Send private message MSN Messenger
sloppy
Expert Cheater
Reputation: 0

Joined: 17 Aug 2008
Posts: 123

PostPosted: Fri Dec 26, 2008 5:41 pm    Post subject: Reply with quote

develito wrote:
Code:
PostMessage(hWnd, WM_KEYDOWN, (int)Keys.A, 0);

Was great!

But the thing i dont get is: How do i tell it to hold down CTRL while pressing a key?

I searched on Msdn but couldn't fint out how to do that.

You can try fiddling with AttachThreadInput & SetKeyboardState. I've created an example below that will send Ctrl+F to Notepad (note: the find dialogue won't open if there is no text),

Code:
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);

[DllImport("user32.dll")]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

[DllImport("user32.dll")]
static extern bool GetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

const uint WM_KEYDOWN = 0x100;

Code:
public void SendCTRLxKey() // Ctrl+F to Notepad
{
    Process[] processes = Process.GetProcessesByName("notepad");

    if (processes.Length > 0)
    {
        IntPtr hWnd = FindWindowEx(processes[0].MainWindowHandle, IntPtr.Zero, "Edit", null);

        uint curThreadId = GetCurrentThreadId();
        uint winThreadId = GetWindowThreadProcessId(hWnd, IntPtr.Zero);

        AttachThreadInput(curThreadId, winThreadId, true);
        this.Focus();

        byte[] kbState = new byte[256];
        GetKeyboardState(kbState);

        kbState[(int)Keys.ControlKey] |= 0x80;
        SetKeyboardState(kbState);

        PostMessage(hWnd, WM_KEYDOWN, (int)Keys.F, 0);
        Thread.Sleep(20);

        kbState[(int)Keys.ControlKey] &= 0x00;
        SetKeyboardState(kbState);

        AttachThreadInput(curThreadId, winThreadId, false);
    }
}

Results might vary from window to window. I've only sent the key down message for F, you may wish to add the key up and Ctrl key down/up messages also.
Back to top
View user's profile Send private message
Tyggo
Expert Cheater
Reputation: 0

Joined: 03 Jan 2008
Posts: 186

PostPosted: Sun Dec 28, 2008 2:33 am    Post subject: Reply with quote

Here's how I did it.

make a timer.

Set the timer interval in the properties.

In the form
Code:
Timer1.Enabled = true;


In the timer code
Code:
SendKeys.Send("Hello World!");


OR in the timer you could put....


Code:
SendKeys.Send ("({enter})");


Or any other key...
Back to top
View user's profile Send private message
Deve
Expert Cheater
Reputation: 0

Joined: 20 Jan 2007
Posts: 245
Location: Stockholm, Sweden

PostPosted: Tue Dec 30, 2008 8:14 am    Post subject: Reply with quote

Tyggo wrote:
Here's how I did it.

make a timer.

Set the timer interval in the properties.

In the form
Code:
Timer1.Enabled = true;


In the timer code
Code:
SendKeys.Send("Hello World!");


OR in the timer you could put....


Code:
SendKeys.Send ("({enter})");


Or any other key...


SendKeys sends messages to the focused window, and in the first post i stated that i want a unfocused window
Back to top
View user's profile Send private message MSN Messenger
FullyAwesome
I post too much
Reputation: 0

Joined: 05 Apr 2007
Posts: 4438
Location: Land Down Under

PostPosted: Tue Dec 30, 2008 5:25 pm    Post subject: Reply with quote

develito wrote:
SendKeys sends messages to the focused window, and in the first post i stated that i want a unfocused window


PostMessage.

_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites