PDA

View Full Version : تایپ متن



mosi20
سه شنبه 23 تیر 1394, 12:28 عصر
سلام
دنبال یک سورس هستم که یک متن چند خطی ( فارسی و انگلیسی مخلوط ) را در یک جای دیگه ( در خارج از برنامه ) که با موس کلیک کردم تایپ کنه
تایپ حرف به حرف را پیدا کردم اما در مورد شکل ها و حروف ترکیبی مثل @ یا آ نتوستم چیزی پیدا کنم البته اونم به صورت جدا پیدا کردم که مثلا @ را چجوری بزنم ( اول شیفت را بگیرم و بعد ۲ را بزنم اما این مدلی برای هر حرف باید خیلی وقت صرف بشه
آیا تابعی یا چیزی هست که تایپ را به صورت سریع انجام بده
متنی که لازم هست تایپ بشه در یک متغیر در برنامه هست

DOT DARK
سه شنبه 23 تیر 1394, 22:24 عصر
تابع SendKeys فکر کنم به دردت بخوره
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys%28v=vs.110%29.aspx

aliagamon
چهارشنبه 24 تیر 1394, 10:03 صبح
واسه اینکار sendkeys اصلا کار اصولی نیست که دارین انجام میدین از sendInput استفاده کنید.
نیاز به یه هوک ساده داره نمونه اینجا هست: http://stackoverflow.com/questions/8884763/how-to-send-a-string-to-other-application-including-microsoft-word

DOT DARK
چهارشنبه 24 تیر 1394, 16:09 عصر
واسه اینکار sendkeys اصلا کار اصولی نیست که دارین انجام میدین از sendInput استفاده کنید.
نیاز به یه هوک ساده داره نمونه اینجا هست: http://stackoverflow.com/questions/8884763/how-to-send-a-string-to-other-application-including-microsoft-word

لطفا دلیل اینکه گفتید "اصولی نیست" رو بگید تا دوستمون بهتر انتخاب کنه.

mosi20
چهارشنبه 24 تیر 1394, 16:35 عصر
SendInput را دیدم اما متوجه نشدم چجوری ازش استفاده کنم
میشه یه مثال بزنید و اینکه متن چند خطی را هم تایپ میکنه؟

aliagamon
چهارشنبه 24 تیر 1394, 17:55 عصر
لطفا دلیل اینکه گفتید "اصولی نیست" رو بگید تا دوستمون بهتر انتخاب کنه.

اصولی نیست چون کارو به شدت سخت میکنه و اجرای چیزی که با 1 خط قابل اجراست با sendkeys خودش یه کلاس کامل میشه.

استفاده ازش اسونه . براتون یه لینک قرار دادم اگه شما اینو تو پروژه خودتون کپی کنید(البته پیشنهاد میکنم به عنوان کلاس اضافه کنید):

/// <summary>
/// Synthesizes keystrokes corresponding to the specified Unicode string,
/// sending them to the currently active window.
/// </summary>
/// <param name="s">The string to send.</param>
public static void SendString(string s)
{
// Construct list of inputs in order to send them through a single SendInput call at the end.
List<INPUT> inputs = new List<INPUT>();

// Loop through each Unicode character in the string.
foreach (char c in s)
{
// First send a key down, then a key up.
foreach (bool keyUp in new bool[] { false, true })
{
// INPUT is a multi-purpose structure which can be used
// for synthesizing keystrokes, mouse motions, and button clicks.
INPUT input = new INPUT
{
// Need a keyboard event.
type = INPUT_KEYBOARD,
u = new InputUnion
{
// KEYBDINPUT will contain all the information for a single keyboard event
// (more precisely, for a single key-down or key-up).
ki = new KEYBDINPUT
{
// Virtual-key code must be 0 since we are sending Unicode characters.
wVk = 0,

// The Unicode character to be sent.
wScan = c,

// Indicate that we are sending a Unicode character.
// Also indicate key-up on the second iteration.
dwFlags = KEYEVENTF_UNICODE | (keyUp ? KEYEVENTF_KEYUP : 0),

dwExtraInfo = GetMessageExtraInfo(),
}
}
};

// Add to the list (to be sent later).
inputs.Add(input);
}
}

// Send all inputs together using a Windows API call.
SendInput((uint)inputs.Count, inputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
}

const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_UNICODE = 0x0004;
const uint KEYEVENTF_SCANCODE = 0x0008;
const uint XBUTTON1 = 0x0001;
const uint XBUTTON2 = 0x0002;
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;

struct INPUT
{
public int type;
public InputUnion u;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
/*Virtual Key code. Must be from 1-254. If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.*/
public ushort wVk;
/*A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character which is to be sent to the foreground application.*/
public ushort wScan;
/*Specifies various aspects of a keystroke. See the KEYEVENTF_ constants for more information.*/
public uint dwFlags;
/*The time stamp for the event, in milliseconds. If this parameter is zero, the system will provide its own time stamp.*/
public uint time;
/*An additional value associated with the keystroke. Use the GetMessageExtraInfo function to obtain this information.*/
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}

[DllImport("user32.dll")]
static extern IntPtr GetMessageExtraInfo();

[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

هرجا که خواستین میتونید تابع SendString رو فراخونی کنید و متنتونو ارسال کنید

mosi20
چهارشنبه 24 تیر 1394, 18:11 عصر
ممنون استفاده کردم ازش
دستتون درد نکنه

mosi20
چهارشنبه 24 تیر 1394, 19:03 عصر
از SendInput استفاده کردم اما تو برنامه ی دیگه تایپ نکرد ( توی بلو استکس تایپ نکرد )
اما تو ویندوز درست تایپ کرد
چیکار کنم؟

DOT DARK
چهارشنبه 24 تیر 1394, 22:05 عصر
اصولی نیست چون کارو به شدت سخت میکنه و اجرای چیزی که با 1 خط قابل اجراست با sendkeys خودش یه کلاس کامل میشه.

استفاده ازش اسونه . براتون یه لینک قرار دادم اگه شما اینو تو پروژه خودتون کپی کنید(البته پیشنهاد میکنم به عنوان کلاس اضافه کنید):

/// <summary>
/// Synthesizes keystrokes corresponding to the specified Unicode string,
/// sending them to the currently active window.
/// </summary>
/// <param name="s">The string to send.</param>
public static void SendString(string s)
{
// Construct list of inputs in order to send them through a single SendInput call at the end.
List<INPUT> inputs = new List<INPUT>();

// Loop through each Unicode character in the string.
foreach (char c in s)
{
// First send a key down, then a key up.
foreach (bool keyUp in new bool[] { false, true })
{
// INPUT is a multi-purpose structure which can be used
// for synthesizing keystrokes, mouse motions, and button clicks.
INPUT input = new INPUT
{
// Need a keyboard event.
type = INPUT_KEYBOARD,
u = new InputUnion
{
// KEYBDINPUT will contain all the information for a single keyboard event
// (more precisely, for a single key-down or key-up).
ki = new KEYBDINPUT
{
// Virtual-key code must be 0 since we are sending Unicode characters.
wVk = 0,

// The Unicode character to be sent.
wScan = c,

// Indicate that we are sending a Unicode character.
// Also indicate key-up on the second iteration.
dwFlags = KEYEVENTF_UNICODE | (keyUp ? KEYEVENTF_KEYUP : 0),

dwExtraInfo = GetMessageExtraInfo(),
}
}
};

// Add to the list (to be sent later).
inputs.Add(input);
}
}

// Send all inputs together using a Windows API call.
SendInput((uint)inputs.Count, inputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
}

const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_UNICODE = 0x0004;
const uint KEYEVENTF_SCANCODE = 0x0008;
const uint XBUTTON1 = 0x0001;
const uint XBUTTON2 = 0x0002;
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;

struct INPUT
{
public int type;
public InputUnion u;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
/*Virtual Key code. Must be from 1-254. If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.*/
public ushort wVk;
/*A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character which is to be sent to the foreground application.*/
public ushort wScan;
/*Specifies various aspects of a keystroke. See the KEYEVENTF_ constants for more information.*/
public uint dwFlags;
/*The time stamp for the event, in milliseconds. If this parameter is zero, the system will provide its own time stamp.*/
public uint time;
/*An additional value associated with the keystroke. Use the GetMessageExtraInfo function to obtain this information.*/
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}

[DllImport("user32.dll")]
static extern IntPtr GetMessageExtraInfo();

[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

هرجا که خواستین میتونید تابع SendString رو فراخونی کنید و متنتونو ارسال کنید

مطمعنین؟؟؟ SendKeys فقط یه تابع سادست توی System.Windows.Forms و فکر نمیکنم اونطوری که شما میگین پیچیده باشه!! من ازش توی پروژه ی Remote Control استفاده کردم و مشکلی نداشته! در ضمن اگه اصولی نیست چه دلیل داشته که مایکروسافت همچین تابعی رو ایجاد کنه؟؟

mosi20
چهارشنبه 24 تیر 1394, 22:24 عصر
میگم این SendKeys چرا با علامت ها مشکل داره؟
خط بعد نمیره خودش
یه تابع ندارید که با کمک SendKeys متن را خودش درست تایپ کنه؟
مثلا پرانتز را تایپ نمیکنه با +9 بهش بدم تا تایپ کنه
اما SendInput با این تابعی که دوستمون داد خود تایپ را درست انجام میده اما توی محیط بلو استکس تایپ نمیکنه
میشه بازم کمک کنید؟

aliagamon
پنج شنبه 25 تیر 1394, 07:01 صبح
بله با این روش تو bluestacks امکان تایپ نیست . چون تو روش بالا با api های ویندوز داریم sendinput یا در اصل Sendmessge میکنیم ولی blustacks یه جورایی مثل virtual machine عمل میکنه پس چیزی که تو این برنامه در حال اجراست برنامه ویندوزی نیست بلکه یک سیستم عامل دیگشت (تا حالا باهاش کار نکردم اما اساس کار این برنامه ها همینه) شما از spy++ خود ویژوال استودیو استفاده کنید ببینید موفق میشین به نتیجه برسین یا نه (tools->spy++)