Hi I am making an application sort of like a mouse macro where when you press F it will move the mouses position between two spots, everything works the only issue I am running into is that I want this to work in the background as in when the form isnt pulled up on my screen. How can I do this? If anyone can help me that would be awesome.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F)
{
PointConverter pc = new PointConverter();
Point pt = new Point();
pt = (Point)pc.ConvertFromString("60, 700");
Cursor.Position = pt;
System.Threading.Thread.Sleep(5000);
pt = (Point)pc.ConvertFromString("600, 700");
Cursor.Position = pt;
}
The events only apply to the form while its active. You cannot use events in a form in the background. Instead use the GetKeyState function.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=vs.85).aspx
Take a look at virtual key codes too. I made an application that listens for key presses in the background once. I had to use GetKeyState in a loop that checked every virtual key code to see if it has been pressed. But sounds like for your application you just need to check for the hot keys you want to listen out for.
You'll need to run a thread in the background for your loop or use a timer maybe.
-Edit-
Here's some more code to implement the GetKeyState function.
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern byte GetKeyState(int nVirtKey);
Related
I have a TextBox in a WPF project where I am trying to detect a mouse click anywhere on the Application other than in the TextBox.
Here is the code that I have so far.
System.Windows.Input.MouseButtonEventHandler clickOutsideHandler;
public MyClass() {
clickOutsideHandler = new System.Windows.Input.MouseButtonEventHandler(HandleClickOutsideOfControl);
}
private void StartCapture() {
System.Windows.Input.Mouse.Capture(TextBox1, System.Windows.Input.CaptureMode.SubTree);
AddHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler, true);
}
private void HandleClickOutsideOfControl(object sender, System.Windows.Input.MouseButtonEventArgs e) {
ReleaseMouseCapture();
RemoveHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler);
}
The problem I'm having is that the event handler never gets called. I've tried capturing the return value of the Capture() function, but it shows as true. Can anyone show me what I'm doing wrong?
You could instead use LostFocus / LostKeyboardFocus but there has to be another element on the window that can get focus.
Second approach that does more what you what exactly ( yet doesn't make total sense) would be to attach to the global mouse down. Intercept every mouse click to WPF application
then on that one do a hittest and determine what is underneath.https://msdn.microsoft.com/en-us/library/ms608753(v=vs.110).aspx
I'm trying to find out how to making my form invisible when I press insert and when I press insert again it makes the form visible. I try to find out how, but no one seems to have what I am looking for.
A simple example how you can manipulate Form's visibility by handling the Insert key down:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Don't forget to enable Form.KeyPreview in order to receive key down events
if (e.KeyCode == Keys.Insert)
{
Visible = false;
}
}
}
You can set Visible back to true to make it visible. However, you will not be able to do this, because Form became invisible and doesn't receive key down events anymore. In this case you can try to set the global hotkey using, for example, the GlobalHotKey library described here. Note also, that it doesn't make sense to set a single key (e.g. Insert) as global hotkey as in most cases system or another application will capture it.
You can do this:
private void InfoForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert) Opacity = Opacity == 0 ? 1 : 0;
}
For this to work you need to turn on the Form's KeyPreview property!
But turning visiblity back on (or to be precise turning off opacity) will only work if no other program has received focus in the meantime.
If that might happen you need to set a global keyboard hook; make sure to pass the Insert key back on or else many other programms will no longer work right.. All in all I would not recommend it..
I'm not sure when the whole idea might make sense. One answer could be to show or hide a popup data window that is only meant to show some additional information popping up from a base data window.
In that case you could simply close the window whenever it gets deactivated:
private void InfoForm_Deactivate(object sender, EventArgs e)
{
this.Close();
}
Well, so first of all I am pretty new to programming in Windows Form Application, so if I lack knowledge I hope you'll understand.
The program I am currently doing is a virtual piano in Windows Form Application. There are buttons on the screen, each button represent a piano "button", if one is clicked - it plays a sound. Now, what I wanted to know is if there is any way that I can program my piano into detecting a continuous click and play sound until the button is "unclicked". For example, if one keeps on clicking on the "G" chord button it will keep playing the sound until he stops clicking.
If I didn't provide any necessary information I would love to know. Thanks in advance to all the answers.
The thing you are looking for is "hold button event".
You could handle the MouseDown and MouseUp events, something like this:
private bool buttonDown;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
buttonDown = true;
int num = 0;
do
{
num++;
label1.Text = num.ToString();
Application.DoEvents();
} while (buttonDown);
}
private void btn1_MouseUp(object sender, MouseEventArgs e)
{
buttonDown = false;
}
This will starts executing the code when you click the button and keeps executing it until you release the button
You need to use mousedown event. When mouse will enter the button the you need to raise the button click event unit the mouseup event fire.
here i'm trying to make a program that when i press the keyboard "F6", it would auto. move the cursor to a position and click. I tested my program on desktop, it works. But when I go into a game and press F6, it doesn't seem to be working. Some people work, some people does not. I was thinking is there any like keypreview priority that I can do?
private async void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
if (IsKeyPushedDown(Keys.F6))
{
if (auth == 0)
{
MessageBox.Show("请先登录");
timer1.Start();
return;
}
SendKeys.Send("{ESC}");
//Original
if (rdbtnoriginal.Checked == true)
{
await Task.Delay(5000);
hack();
}
}
timer1.Start();
}
Here I use a timer tick, I got the code from online, so when I'm not focusing on the form and press F6 it will trigger the event. But some people go into the game and press F6 it doesn't work
Winforms relies on the window being focused for key messages to register in the application (if the window isn't focused, the IsKeyPushedDown won't register any keys as the window hasn't recieved the keypressed message in the background. you may want to use the input features from DXInput or OpenGL, or have a look at this http://www.codeproject.com/Articles/18890/NET-Hookless-Key-logger-Advanced-Keystroke-Mining. there are probably other libraries/pieces of code. Google is your friend, Key logging is probably your best search term.
I am using the below code to capture the ctrl + alt + Q hot keys, which works perfectly.
But, i want to use this in a background application. Since my application does not have any forms, i want to use the same code inside a class file.
i am confuse, because i cannot write a event handler [keypressed] in class file.
Instead i want to use the keypress in thread.
Please help.
public DialogResult Result;
KeyboardHook hook = new KeyboardHook();
public Form1()
{
InitializeComponent();
// register the event that is fired after the key press.
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
// register the control + alt + F12 combination as hot key.
hook.RegisterHotKey((ModifierKeys)2 | (ModifierKeys)1, Keys.Q);
}
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
Result = MessageBox.Show("Are you sure, you want to log off?","Log off"
,MessageBoxButtons.YesNo
,MessageBoxIcon.Warning);
if (Result == DialogResult.Yes)
{
}
else
{
}
}
If you want to capture a global hotkey without a form, I'm afraid you cannot.
The reason is that global hotkeys are sent to a window handle (and processed in wndProc, aka. the message pump)
So basically the way Windows works, you cannot use global hotkeys without a form to received them.
I'm not entirely certain this is what you want to do however. But on the other there won't be any local hotkeys without a form either, so I cannot see what else it might be.
You may want to further clarify your question a bit (no offense)