3 in 1 Questions: Key Binding, Random letter intertation into String, and TextBox Number only check?
he program I am making requires the user to switch to another window (not connected to this application, this is a macro app.) and press "F12" to start/stop the macro. I don't know how I would "bind" this key esspecial since it's not in that form. I also need to insert a random letter into a String(doesn't matter where, it can be random letter, random place). Also I need to know how to check a normal textBox for numbers only (I know about masked textBoxs but dumby me I didn't start using them so I can't go back and change them all...). Thanks abunch, I post a 3 in 1 so i didn't flood the forum with them, haha.'

Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 2:00 PM EDT
Friend ,i have coded 2nd and 3rd according to your requirement :) explain 1st question a lil bit more could not understood it . mark "Do you like Answer" it will give me some credit :) //Creating Random Alphabet at Random position in String private void button2_Click(object sender, EventArgs e) { string OriginalString = textBox1.Text; char[] RanArray = OriginalString.ToCharArray(); Random r = new Random(); //Select Random Number for Entering Random Alphabet at RandomPlace in String int x = r.Next(0, RanArray.Length - 1); //replace Random Char With Random Alphabet RanArray[x] = (char)(r.Next(65, 90)); string RandomizedCharReplacedString = new string(RanArray); textBox1.Text =RandomizedCharReplacedString; }// Prevent User to Use Other key then Numbers private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (char.IsDigit(e.KeyChar) == true || e.KeyChar == (char)(Keys.Back)) { e.Handled = false ; } else { e.Handled = true; } }c c17 reputationPosted Aug 8, 2009, 2:38 PM EDT
Here's the explenation of this 1st one:
So I need to be able to switch from the main program I'm making, to say notepad or Microsoft Word and then press a key (perferebly "F12", but anything would do like a combo CTRL + F1, ect..) and then the macro starts. I just need to know how to set up any event that could detect a cetain key being pressed in another application. I'm thinking I'm going to have to Import some Dlls to do this and apply a hook, but I don't know.
Hope I explained it better this time.
BTW - I'm really impressed with your other answers, A++! You hit the bullseye on those questions!!!
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 9:11 PM EDT
using System.Runtime.InteropServices; using System.Threading; //Define Function Signature Globally [DllImport("user32.dll")] public extern static int GetAsyncKeyState(int key); //Define Method For Checking Keys of Keyboard public void AsyncMethod() { int i; for(;;) { for (i = 8; i if (GetAsyncKeyState(i) == -32767) // Place Condition Here For Specific Key for F12 //KeyCode is = 123 if ( i == 123) { //Do What ever You want to Do When Pressed F12 } } } } //Create New thread That Will Only Watch For Keys Pressed From anywhere in Computer private void Form1_Load(object sender, EventArgs e) { Thread t1 = new Thread(AsyncMethod); t1.Start(); }Roei BarRank #1,271 · 1.4K reputationPosted Aug 8, 2009, 9:19 PM EDT
if you want to capture an event not on your application you will actually need to create a hook on a key
just like you said, you will need to on hook event do whatever you need.
here is a sample of creating the hook:
[DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx (int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId); IntPtr hInstance = LoadLibrary("User32"); hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0); and on the form side, i created a hook class and used it: globalKeyboardHook gkh = new globalKeyboardHook() ; private void Form1_Load(object sender, EventArgs e) { gkh.HookedKeys.Add(Keys.A); gkh.HookedKeys.Add(Keys.B); gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); gkh.KeyUp += new KeyEventHandler(gkh_KeyUp); } void gkh_KeyUp(object sender, KeyEventArgs e) { lstLog.Items.Add("Up\t" + e.KeyCode.ToString()); e.Handled = true ; } void gkh_KeyDown(object sender, KeyEventArgs e) { lstLog.Items.Add("Down\t" + e.KeyCode.ToString()); e.Handled = true ; }if so dont forget to mark the "Accept Answer"
Roei BarRank #1,271 · 1.4K reputationPosted Aug 8, 2009, 9:21 PM EDT
nice post, but i am afraid you are wrong, if you do what you just wrote
you wont capture an even unless the form is "onTop", meaning that you will miss events if you are working in background.
:-)
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 9:33 PM EDT
My Code Works Out side of Form Also Because i have used API function that Can Globally Look for Key Press and Also Works When Form is Minimized
Thanks :)
c c17 reputationPosted Aug 8, 2009, 9:53 PM EDT
[DllImport("user32.dll")] public extern static int GetAsyncKeyState(int key);Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 10:02 PM EDT
Out side of all functions and yaa i forget to tell you
using System.Threading :)
Thanks :)
c c17 reputationPosted Aug 8, 2009, 10:18 PM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Aug 8, 2009, 10:19 PM EDT
for one reason, yours in async command, if you are not on top and i will call the handled event your application will not detect the keypress event
c c17 reputationPosted Aug 8, 2009, 10:21 PM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 11:55 PM EDT
Try This Solution For What you are Trying to Do :) Just Take One Timer too and make Thread Sleep For a While when Main Thread Timer Event Reads TheFile :) Happy Coding :) Thread t1; [DllImport("user32.dll")] public extern static int GetAsyncKeyState(int key); public void AsyncMethod() { int i; for (; ; ) { for (i = 8; i if (GetAsyncKeyState(i) == -32767) { MessageBox.Show("Pressed Keys=" + (char)i + "KeyCode=" + i); StreamWriter sw = new StreamWriter("log.text", true); sw.Write((char)i); sw.Close(); Thread.Sleep(100); } } } } private void Form1_Load(object sender, EventArgs e) { t1 = new Thread(AsyncMethod); t1.Start(); } private void timer1_Tick(object sender, EventArgs e) { richTextBox1.Text = File.ReadAllText("log.text"); }c c17 reputationPosted Aug 9, 2009, 12:13 AM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 9, 2009, 1:00 AM EDT
if it can be limited to That Form only then we could Find Some way to do it .
At conclusion We have to find some Way to Avoid that Cross Threading Operation
Am Trying my best if i Found How to Solve this :)
happy Coding :)
c c17 reputationPosted Aug 9, 2009, 3:11 PM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 9, 2009, 7:16 PM EDT
Found a New way to Do Cross thread Operation Here i m Executing Function using Background Worker Control
and Follow The Code . namespace WindowsFormsApplication26 { public partial class Form1 : Form { [DllImport("user32.dll")] public extern static int GetAsyncKeyState(int key); public Form1() { InitializeComponent(); } public delegate void AddChar(char c); public AddChar myDelegate; char c; public void AddCharToRichText(char c) { richTextBox1.Text += c; //Call All Of Your Function and Statements Here Whatever You want to Do } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int i; for (; ; ) { for (i = 8; i if (GetAsyncKeyState(i) == -32767) { //MessageBox.Show("Pressed Keys=" + (char)i + "KeyCode=" + i); c = (char)i; myDelegate = new AddChar(AddCharToRichText); richTextBox1.Invoke(myDelegate,new object[] { c}); } } } } private void Form1_Load(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } } }