I have an application that displays a keyboard and tests whether the keys have been pressed or not. The issue I'm having is that when certain keys are pressed like the arrow buttons/tab, the keyboard graphic loses focus and it starts accessing menu items/etc. I tried registering to the preview mouse down event in the MainWindow and setting e.handled = true. But this does not work all the time. It would also be nice if there was a way to disable the windows button as well.
I think you would need to get into the operating system code for your solution. The OS treats some keys different from normal, so you may not be able to peak at a key's value or even that it has been pressed before it takes control away from your application.
I saw this kind of thing back when I was writing machine code BIOS routines for CP/M. Windows is much more involved than that. I quit writing code to control hardware when I started to use Windows 3.1.
I used this class:
http://gist.github.com/471698
I replaced line 99 with this code:
return EnableKeyboard ? InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam) : (IntPtr) 1;
Where EnableKeyboard is set by the user.
Related
Good evening folks,
I'm building a simple application (A) that sends Strings to a textbox of another application (B). I was able to this step, but afterwards I'd like to automatically press a button placed just under the textbox. The problem is that I can't get the Handle of the Button; using "Window Detective"(similiar to Spy++), I see only the textboxes (called "TEdit", see the attachments) and no Buttons!. I'd like to add also that there's no only a Button but 3!! So, how could I press a specific Button? Is there another chance to get the Handle?
Program "target"
Window Detective screenshot
Based on the class name TEdit that's a VCL application probably coded in Delphi. The buttons are likely TSpeedButton and non-windowed. You won't be able to send them messages and they are not automatable.
Faced with this your best hope of success is to fake input. Fake the mouse click at the appropriate location on the form. It's not pretty but there's little option.
I am writing a WPF application that will put an icon in the system tray and, as an exercise, I want to do this without depending on System.Windows.Forms and using its NotifyIcon or NativeWindow classes.
This is fairly easy - Shell_NotifyIcon isn't hard to call from C# - and, indeed, I have succeeded in my task.
As part of this effort, I have had to create a window handle for the sole purpose of receiving messages from the system-tray. I create the native window as follows:
// Create a 'Native' window
_hwndSource = new HwndSource(0, 0, 0, 0, 0, 0, 0, null, parentHandle);
_hwndSource.AddHook(WndProc);
The message loop is hooked in AddHook() and messages are processed in a function that looks like this:
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle windows messages in this...
}
And, finally, when it comes time to destroy the thing, I close the window by posting it a WM_CLOSE message and disposing the HwndSource.
if (null != _hwndSource)
{
UnsafeNativeMethods.PostMessage(_hwndSource.Handle, WindowMessage.WM_CLOSE, 0, 0);
_hwndSource.Dispose();
_hwndSource = null;
}
My question is this: the first three parameters to the constructor of HwndSource are the class style, style and extended style of the native Win32 window, respectively. For a non-visible window that will only be used as a target for window-messages, what should they be?
My defaults of zero, zero and ... er.. zero do work but I have used Spy++ to examine what Windows.Forms.NotifyIcon does and it seems that the NativeWindow it creates have the following:
Class Style: <zero>
Styles: WS_CAPTION, WS_CLIPSIBLINGS,
WS_OVERLAPPED
Extended Styles: WS_EX_LEFT, WS_EX_LTRREADING,
WS_EX_RIGHTSCROLLBAR, WS_EX_WINDOWEDGE
Are any of those important for a non-visible window? (I think not.)
Windows style flags date from 1986, back when Windows v1.0 was released. There have been lots of appcompat hacks in the past 29 years and 10 major versions, Windows silently overrides style flags when the app specifies wonky ones. Nothing terribly wonky about this however, note that the value of the WS_OVERLAPPED style flag is 0. Which asks for a plain window, you automatically get the appropriate style flags for such a window.
Your HwndSource window has the exact same style flags, maybe you haven't found the correct one back in Spy++. So you don't have a problem. And no, they don't matter when the window never becomes visible.
Note a bug in your code, the WM_CLOSE message you post is never actually processed since you destroy the window right after calling PostMessage(). Just delete it, there is no point in asking the window nicely, it isn't going to object. You do however have to call Shell_NotifyIcon() with NIM_DELETE to delete the tray icon. Failure to do so leaves a "ghost" icon that only disappears when you move the mouse over it.
And do note that NotifyIcon is not as trivial as you assume it is, it has a non-obvious bug workaround that you are likely to overlook. You'll notice when the context menu refuses to close.
I'm building a "WPF Application" which is made to be run in the background (minimised state) and detects KeyStrokes of each and Every key on the keyboard & every Mouse Clicks.
So, my question is how to detect every keyStrokes whether app (Window) is minimised or not.
Simply, if my app is in focus then i use this code to count keystrokes.
Public int count;
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
//base.OnKeyDown(e);
count++;
tBlockCount.Text = count.ToString();
}
I just want to do the same even if my app is minimised.
I've searched a lot and come across many suggestions like..
http://www.pinvoke.net/default.aspx/user32/registerhotkey.html
http://social.msdn.microsoft.com/Forums/vstudio/en-US/87d66b1c-330c-42fe-8a40-81f82012575c/background-hotkeys-wpf?forum=wpf
Detecting input keystroke during WPF processing
Detect if any key is pressed in C# (not A, B, but any)
Most of those are indicating towards Registering HotKeys. But I'm unable to match scenario with mine.
Any kind of suggestion are most welcome.
Although I'm not really condoning the use of a keylogger (This is what you are trying to do). I would recommend taking a look at this q/a, the section near the bottom of this article, and this article for some inspiration. These should help point in the right direction for the coding side.
What you essentially need to do is just set up an event to intercept any keys that come in from the computer, then you can gather the key and do whatever you like with it (in your case, record it)
Edit: In fact, reading the third article, it actually gives a full code snippet on how to implement and use it in WPF, so I recommend just reading that one.
i need to find out automationid for key board values? how to send keystrokes to application UI automation? i need to automate page up and page down function in key board. What is the best way to do
it?
EDIT:
in my application following the process. Assume end user open MS Word document with 5 pages and he presses page up and page down buttons to move within these pages. i want to automate this scenario using c#. currently i have uses UIAutomationClient.dll and UIAutomationTypes.dll. can i uses these?
A very good way for automated sending of keystrokes of all kinds is the AutoIt automation and scripting language. Lots can be done with it, especially page up and page down sending.
I suggest writing an EXE with autoit, which connects itself to the program it will send keystrokes to.
Active the application's window using P/Invoke.
Call SendWait("C") with any character.
E.g.
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// Send a series of key presses to the Calculator application.
private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the Calculator application. The window class
// and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
// Verify that Calculator is a running process.
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
// Make Calculator the foreground application and send it
// a set of calculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}
See How to: Simulate Mouse and Keyboard Events in Code > "To send a keystroke to a different application"
I think you want to send keystrokes to an application for which you don't have source code.
I can't help you telling you how to do it directly in C#.
But you can do it very easily with AutoIt; it has a DLL you can reference in C# to do exactly what you need.
Have you read
How to send keys instead of characters to a process?
This tells you exactly how to send keys to an application.
//
use a windows form and use this list with send key
https://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm
I'm pretty sure he's just asking for values of the keys for his SendKey.Send("{}");
in this scenario get the word document to fore ground and then used on screen key board
System.Diagnostics.Process.Start("osk.exe");and click page up and down buttons using mouse input
since for the mouse click need screen coordinates for the page up and down button.
( i tried to detect on screen key board using UI automation. but it did not detect keys of the screen.
https://stackoverflow.com/questions/11077738/windows-sdk-inspect-tool-what-are-the-reasons-on-screen-keyboard-does-not-disp couldn't find solution for this problem.so because of that i use this move click method to click the button. )
I'm trying to add a button to my current project that when pressed will send the window to the back for x-seconds, allow the user to work in other windows, and then automatically come to the front again. By combining How to send a WPF window to the back? and Bring a window to the front in WPF, along with a BackgroundWorker, I was able to get this 99% done. When the button is pressed, the window goes to the back, and returns the specified number of seconds later. The problem is that if I go into another window (Opera, Word, etc), it never return from the back. I tried playing with the flags, but can't seem to get it to work. Is this possible to do? And if so, how?
Thanks!
With a hack it's possible. Just do that:
Topmost = true;
Topmost = false;
and the window should be in front.