Put this on top of your class:
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
Then a method to recieve text.
static uint KEYEVENTF_KEYUP = 2;
static byte VK_CONTROL = 0x11;
public static string gettext()
{
string message;
try
{
Thread.Sleep(100);
message=Clipboard.GetText();
return message;
}
catch(Exception)
{
gettext();
}
}
public static string highlightedtext()
{
string output;
keybd_event(VK_CONTROL,0,0,0);
keybd_event (0x43, 0, 0, 0 );
keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
output=gettext();
return output;
}
NOTE: Sometimes the clipboard is in use and will give exception so you need try and catch , If it catch exception , do it again until it's not.
Advantage of using these code.:
1.If you just want something in the clipboard , call gettext()
2For highlighted text , call highlightedtext()
Anyone see errors please edit my post.
Thank you
EDIT!!: Adding static is use for main(). If you aren't use with main, Remove static
Edit2!!:Adding static for all outside variables!! Please tell me any more error.