String to Byte? Convert.ToByte(textbox1.text)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey guys I have written a small application that will send keys from the
keyboard depending on the users input. My thing is I can't use SendKeys.Send
because i need to emulate the keyboard pressing the keys so I am using
Keybd_Event from user32.dll.

When the user gives their input they place the number they would like to
send into textbox1. I need to some how be able to convert the 0 - 9 to be
0x30 - 0x39. I was thinking I could just use: keybd_event(0x30 +
Convert.ToByte(textBox1.Text), 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); but
that doesn't work. I'm in a rut because I have been spending to much time on
this last detail. Any help would be great! Thank you so much!!!
 
Chris,

Why not create a map where the 0-9 will correspond to the appropriate
VK_ constants? Then, you just perform the lookup, and send that virtual key
code.

Also, you should not be using keybd_event on W2K and XP systems. You
should be using SendInput instead.
 
Nicholas,

Sorry for such a silly question but why should I use SendInput? What DLL is
that called from? I had some help from some friends and got my issues
figured out.

All of this goes in Form1 : Form
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
public const int KEYEVENTF_EXTENDEDKEY = 0x1;
public const int KEYEVENTF_KEYUP = 0x2;
public System.Byte Byte001;

This part goes into the button:
Byte001 = Convert.ToByte(Convert.ToInt16(textBox1.Text) + 48);

And this into the BackGroundWorker1 Do Work
keybd_event(Byte001, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event(Byte001, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, (UIntPtr)0);

I would like your feed back because I would like to know what bugs I may have.
Thank you,
Chris
 
Is there any reason this wouldn't work in all applications? It will work in
notepad and such but when I bring it into the application I want it to send
the key strokes to it doesn't pass the keys proprly. I can use another
application called AutoIT3 and get this to work just fine, but I would of
thought VC# would of not only done the job, but would have done it better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top