How to get keyboard and mouse input (what buttons are currently pressed) && also how to send output (make the mouse click at a specific position in another program). If you could also add comments on what different things do and why you use them would be terrific :D
Cheers! //Daniel
http://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library
This article is quite helpful. It even includes code for a cool macro recorder.
This may not be a very good solution but bad as i am (didnt understand how to use the other ones that were suggested) i chose the siple way out... i used using Microsoft.Xna.Framework.Input; after getting the xna framework reference, then i simply went ahead and did keyboardstate currentkeyboard = keyboard.getstate :) same with mouse
.NET only has .NET controls (forms, buttons,etc) based mouse/keyboard hooking built in, so to programmatically respond to those events outside your .NET controls you need a global event hook that can intercept mouse and keyboard activity from anywhere in windows.
HowTo use one keyboard/mouse hook dll to perform the functionality you are requesting (the ability to respond programmatically to mouse/keyboard events globally):
Download "Gma.UserActivityMonitor.dll" from: global mouse/keyboard kooks in c#
Place this dll in C:\Windows\System32
In your project, include a reference (from VS, add...reference and browse to the dll)
In your project code, add "using Gma.UserActivityMonitor;"
In your code, declare these event consumers:
HookManager.MouseMove += HookManager_MouseMove;
HookManager.KeyPress += new KeyPressEventHandler(HookManager_KeyPress);
Here are some method stubs that can be raised when the mouse/keyboard events occur:
private void HookManager_MouseMove(object sender, EventArgs e)
{
}
private void HookManager_KeyPress(object sender, EventArgs e)
{
}
Hope this helps, this is how I did it when I needed to consume global mouse and keyboard events in my code.
Related
I'm trying to fiddle with an idea, and one big concept is dropping programs, bookmarks, etc. I have what I believe to be the gist of it but I'm not receiving any information
private void Border_Drop(object sender, DragEventArgs e)
{
FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);
foreach (FileInfo file in files)
{
Writer.Text += file.FullName;
}
}
Though my textblock is never populated? I'd like to get all the properties of File such as FullName, Name, Extension, DirectoryName, etc.
Any pointers on where to go from here? I've been trying to read the MSDN of the event but nothing is coming up, and I've searched stackoverflow and nothing is working for my case scenario.
One possible reason is that you have attached the event handler to the wrong control. Try attaching the event handler to the Form, or if that does not work, the textblock.
I would also highly recommend doing some debugging. Place a breakpoint in the event handler to see if it is called, and if so, what happens in it.
WPF has two different routed event handling mechanisms: bubbling and tunneling. Bubbling is the "normal" way to do it, and tunneling is when you see all those PreviewThis and PreviewThat in the event name. It's possible, that another control has aready marked the event as handled, and so you don't see it in your event handler.
If you need documentation for WPF drag and drop, read this
If you need some really custom, really low level stuff on this subject, you can also read up on Object Linking and Embedding This is probably how drag-and-drop is implemented in Word so that you can drag images and Excel tables into Word documents and they will display natively. But to use this you will probably need to use some Win32 API calls which is a bit of a bummer.
I want to intercept a hot-key (specifically CTRL V) which will interact with my application in a certain way. I am able to globally register the hotkey using the RegisterHotKey method. When I press CTRL V I want the focused window/control to also receive the WM_PASTE message. I've tried sending it trough SendMessage but it didn't seem to work. I eventually ended up Unregistering the HotKey, sending ^v to the current window, then re-Registering the hotkey.
private static void Hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
_hook.KeyPressed -= Hook_KeyPressed;
_hook.Dispose();
SendKeys.Send("^v");
_hook = new KeyboardHook();
_hook.RegisterHotKey(global::ClipMaster.ModifierKeys.Control, Keys.V);
_hook.KeyPressed += Hook_KeyPressed;
}
This does the trick, but it blocks the mouse (and the window) for around half a second. I'm also afraid it won't work in certain applications, although I don't know what example I could give.
I'm not sure what you're trying to achieve with that hook since it's impossible to understand it from the code example.
Assuming you do it for auditing reasons (or monitoring of the sort), I'd suggest to review the return value of the WM_HOTKEY message. It can "say" that the message wasn't processed, hence allowing further processing of the Ctrl-V by other logic (e.g. other hooks).
Also, i'd advise to use alternative way to re-send Ctrl-V than SendKeys class which has know timing issues. Did you consider posting a message instead?
I've also found alternative approach to the hooking itself by using lower level key pressing for hooking.
Let me know if it helped.
I'm new into WPF and have a problem I can't seem to find a solution for.
I'm writing a G19 (Keyboard) applet. This Keyboard has a 320x240 display attached, which you can access with C#.
I'm using WPF for this, because I don't need to do any GDI drawing anymore and use the normal controls instead.
So. It works as I wish. Everything draws properly except one UserControl. I have downloaded this control -> http://marqueedriproll.codeplex.com/
In the designer, the control works, the Loaded event get's fired and the animation is good.
When I run my application, I just see the label and the text. The animation does not work, and the Loaded event does not fire anymore.
Any help is appreciated.
The main function is my wrapper. The wrapper is already a Usercontrol and displays plugins which are switchable. This wrapper has the Frame Control(Wrapper1). I replace the content of this frame every time I switch the plugin.
public void SetPlugin(IPlugin plugin)
{
if (this.MainPlugin != null)
{
this.MainPlugin.OnHide();
((UserControl)this.MainPlugin).Visibility = System.Windows.Visibility.Hidden;
}
this.MainPlugin = plugin;
((UserControl)this.MainPlugin).Visibility = System.Windows.Visibility.Visible;
this.MainPlugin.OnShow();
this.Wrapper1.Content = this.MainPlugin;
}
I think it's the right approach to handle a plugin system that way. The plugin get's drawed on my keyboard.
What I don't understand is why the usercontrol only works in the designer view and not in the running application.
The basic code of the scrolling label is so:
public MarqueeText()
{
this.Loaded += new RoutedEventHandler(MarqueeText_Loaded);
InitializeComponent();
canMain.Height = this.Height;
canMain.Width = this.Width;
}
void MarqueeText_Loaded(object sender, RoutedEventArgs e)
{
StartMarqueeing(_marqueeType);
}
I don't see a reason why it doesn't work. Actually Ive always found a way to fix a problem but this time I see nothing.
Thanks in advance. Your help is really required today.
Have a great saturday! :)
I am guessing you are rendering to a bitmap target, rather than onscreen. If you are using RenderTargetBitmap, you have a couple of responsibilities. You need to set both a presentation source, and make sure you run events on the dispatcher.
Normally, App.xaml or Application.Run does this for you, but if you are not using a Window, you are on your own.
See this related question for details.
The AxAcroPDF swallows all key-related events as soon as it gets focus, including shortcuts, key presses, etc. I added a message filter, and it doesn't get any key-related messages either. It's a COM component, could that be relevant?
Is there any way to catch these before the control starts swallowing them?
Hans is correct, the Acrobat Reader spawns two child AcroRd32 processes which you have no direct access to from within your managed code.
I have experimented with this and you have three viable options:
You can create a global system hook, and then look for and filter out / respond to WM_SETFOCUS messages sent to your child AcroRd32 windows. You can accomplish some of this from within C# by using a wrapper library, such as the one here: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
You also need to identify the correct processes as there may be more than one instance of your application, or other instances of AcroRd32. This is the most deterministic solution, but because your application will now be filtering messages sent to every single window in existence, I generally don't recommend this approach because then your program could negatively affect system stability.
Find an alternate PDF viewing control. See this answer for a few commercial components: .net PDF Viewer control , or roll your own: http://www.codeproject.com/KB/applications/PDFViewerControl.aspx
Find an acceptable hack. Depending on how robust your application needs to be, code such as the following may be suitable (it was suitable for my case):
DateTime _lastRenav = DateTime.MinValue;
public Form1()
{
InitializeComponent();
listBox1.LostFocus += new EventHandler(listBox1_LostFocus);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axAcroPDF1.src = "sample.pdf"; //this will cause adobe to take away the focus
_lastRenav = DateTime.Now;
}
void listBox1_LostFocus(object sender, EventArgs e)
{
//restores focus if it were the result of a listbox navigation
if ((DateTime.Now - _lastRenav).TotalSeconds < 1)
listBox1.Focus();
}
I might finally have a ridiculously simple answer. So far in testing this is working.
Having suffered from this problem for quite some time and having built a complex system of each custom control recording which of them last had focus and using a timer to flip focus back (when acropdf grabbed it) I revisited this problem and read a great number of answers (looking for recent solutions). The information gleaned helped me with the idea.
The idea is to disable the (acropdf) control whilst it is loading as in the following example (code reduced for clarity)
AxAcroPDF_this.Enabled = False
AxAcroPDF_this.src = m_src
Then on a timer, after say 1 second.
AxAcroPDF_this.Enabled = True
Basically the idea is to tell Windows not to let users use the acropdf control until allowed, so asking Windows to prevent it from getting focus (because users are not allowed in there).
So far this is holding up, I will edit this if anything changes. If it doesn't work completely for you then maybe the idea points into a useful direction.
It is an out-of-process COM component, that's the problem. Completely in violation of Windows SDK requirements as laid out in SetParent(). Once its window gets the focus, the message loop in the acroread.exe process gets all the messages, your message filter cannot see any messages anymore.
Technically it is fixable by using SetWindowsHookEx() to inject a DLL into the process and monitor messages with WH_GETMESSAGE. But you can't write such a DLL in the C# language.
Major suck, I know. There never seems to be any lack of it with that program.
For some reason Tim's answer, disabling the AxAcroPDF control directly, didn't work in my case. The Leave event on the previously-selected Textbox would never fire, either.
What is working is nesting the AxAcroPDF control inside of a disabled GroupBox. Since the users of my application need to only see the PDF, not interact with it, the GroupBox's Enabled property is set to False in the designer.
I was just wondering if there is a way to disable controls such as ctr Left/Right arrows or Alt+left/right arrows in AxWindowsMediaPlayer. I am using it in WindowsFormsHost in my WPF project. I would like to capture these controls and handle them myself. When I use this block, I don't have any way to prevent it by using e.handeld =true or other ways
void MediaPlayer_KeyDownEvent(object sender, AxWMPLib._WMPOCXEvents_KeyDownEvent e)
{
}
Any suggestion how to disable it and continue with my own controls. The important part is it bubbles up somewhere and freezes the GUI if I use such commands, and I don't have any way to capture it to control. It doesn't have any error though.
thanks
It can be blocked in WindowsFormHost level by controlling keydown.
I have one thought to try, but I haven't tested it. Inherit from WindowsFormsHost and override WndProc() method. In this method capture WM_KEYDOWN message, process it, and if it's a key you want to suppress, return 0.
If it doesn't work, you may want to find other way to hook windows procedure.
Hope this helps.
Cheers, Anvaka