What's new

[VB] Key Binds / Marcos

  • Thread starter xKNiiVES
  • Start date
  • Views 551
X

xKNiiVES

Enthusiast
Messages
590
Reaction score
88
Points
95
Sin$
0
Hey, when I press the numpad "1" key I want it to said Textbox1.Text.

So, if I press numpad key 1 it'll send textbox1.text.

I've tried sendkeys but I don't think that's what I am looking for.
 
Z

Zer0-One

Experienced Member
Messages
2,178
Reaction score
256
Points
220
Sin$
0
try re-stating that. I have no idea what you mean by "send" Textbox1.Text
 
SotG Caboose

SotG Caboose

Getting There
Messages
1,448
Reaction score
687
Points
230
Sin$
0
You would have to find the Windows pointer, then send it a message.

I found this class here: http://stackoverflow.com/questions/407020/sending-keyboard-macro-commands-to-game-windows

It gets the window pointer, and then sends a message to the window.

Code:
using System.Runtime.InteropServices;

//...

class SendKeySample
{
private static Int32 WM_KEYDOWN = 0x100;
private static Int32 WM_KEYUP = 0x101;

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

public static IntPtr FindWindow(string windowName)
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.MainWindowHandle != IntPtr.Zero && p.MainWindowTitle.ToLower() == windowName.ToLower())
return p.MainWindowHandle;
}

return IntPtr.Zero;
}

public static IntPtr FindWindow(IntPtr parent, string childClassName)
{
return FindWindowEx(parent, IntPtr.Zero, childClassName, string.Empty);
}

public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key)
{
PostMessage(hWnd, WM_KEYDOWN, key, 0);

}
}

To make it send every char in your textBox you would do this:

Code:
IntPtr hWnd = SendKeySample.FindWindow("Untitled - Notepad"); // Notepad handle
IntPtr editBox = SendKeySample.FindWindow(hWnd, "edit"); // The textbox in notepad

for (Int32 i = 0; i < this.textBox1.Text.Length; i++)
{
SendKeySample.SendKey(editBox, (Keys)this.textBox1.Text[i]); // Cast the char in the textbox as a key and send it to the editBox
}
 
Top Bottom
Login
Register