Windows Develop Bookmark and Share   
 index > Windows Forms General > Recieving Keyboard Presses From a Program
 

Recieving Keyboard Presses From a Program

How would I do this? I have limited C# experience and I know how to use SendKeys command Here is some sample code of what im trying to do but I cant get it to work.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Macro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void ButtonIsPressed(KeyPressEventArgs keypress)
{
KeyPreview = true;// just figured I might need this for some reason
/*Removed in case of was causing problems
if (checkBox1.Checked && keypress.KeyChar.Equals(Keys.W))
{
//My SendKeys here
}
*/
if (keypress.KeyChar.Equals(Keys.W))//Right here is the problem I believe...
{
SendKeys.SendWait("H");
SendKeys.SendWait("o");
SendKeys.SendWait("w");
SendKeys.SendWait(" ");
SendKeys.SendWait("T");
SendKeys.SendWait("o");
SendKeys.SendWait(" ");
SendKeys.SendWait("D");
SendKeys.SendWait("o");
SendKeys.SendWait(" ");
SendKeys.SendWait("T");
SendKeys.SendWait("h");
SendKeys.SendWait("i");
SendKeys.SendWait("s");
SendKeys.SendWait("?");
}

}
}
}



Vbnmzzz  Thursday, February 01, 2007 2:39 AM
Ok I got it I basically just took a keylogger source and removed some parts and added in what I needed for it to work. Thanks for the help
Vbnmzzz  Friday, February 02, 2007 5:36 AM

SendKeys to send keystrokes and keystroke combinations to the active application

so you have to bring the application that is going to receive messages to foreground

take a look at SetForegroundWindow and same on pinvoke.net

Hope this helps

Galin Iliev  Thursday, February 01, 2007 9:15 AM
Thats not the problem I'm having I cant get it to do the
private void ButtonIsPressed(KeyPressEventArgs keypress)
{
if (keypress.KeyChar.Equals(Keys.W))//Right here is the problem I believe...
{
SendKeys.SendWait("s");
}
}
I run the program and when I press the W key it does not do anything.
I also have tried placing a textbox in the form and then pressing W and then it should type in "s" right? But no it doesnt I'm trying to make it so if I press a letter key on the keyboard then it sends keys automatically.

if (keypress.KeyChar.Equals(Keys.W))
Vbnmzzz  Thursday, February 01, 2007 9:31 PM
Ok I got it I basically just took a keylogger source and removed some parts and added in what I needed for it to work. Thanks for the help
Vbnmzzz  Friday, February 02, 2007 5:36 AM
hello i am ryan i have recently lost my disk clean up icon and wondering if any 1 can help me sorry to poast on here=P
disk clean up icon is gone  Sunday, May 13, 2007 1:06 AM

You can use google to search for other answers

Custom Search

More Threads

• Drawing to a non-static panel from a static class.
• Problem with mdi. Close active form and not open already opned form??
• exiting a form
• DragDrop funcionality C# with Windows VISTA
• where could i find a good rich text box control that I could use in VB.NET?
• How do i pass control to another form in a button click event
• copy
• Adding BLOB images to a ImageList
• custom UserControl
• Draw points