Handling print-screen globally in Windows by a .Net application - c#

How can I make my C# application the default "Print Screen" handler in Windows?
I wrote a screen capturing utility and I want to, ideally, have it replace the default print-screen handler, or otherwise have a unique key combination that would trigger it.
I know how to do it in C++ using global hooks, etc., but it's not clear to me how this can be done in .Net. Also, if there's a way of doing it without using a memory resident application that would be super.

As long as you are using a WM_KEYBOARD_LL hook to capture the PrintScreen key, there is no reason you can't do this in C#. It's one of the few global hooks that can be used in managed code because it doesn't require a DLL to be injected into every target process. Instead the context will switch back to the originating application for processing.
Adam's blog has a great post on how to setup such a hook in C#: http://www.seesharpdot.net/?tag=wh_keyboard_ll

Why not use AutoHotKey (OpenSource, free) with the following script?
This script was taken from the newsgroup of AutoHotKey, it will map the printscreen to use IrfanView. You can use it to trigger your program.
~printscreen::
ifexist,%screenshot1%\irfanview.exe
{
run, "%screenshot1%\irfanview.exe" "/capture=0 /convert=%screenshots%\%A_now%_screenshot.jpg"
sleep,500
run,%screenshots% ;open folder
return
}
return
~!printscreen:: ;copy only active window
ifexist,%screenshot1%\irfanview.exe
{

Related

interact with external application in c#

I was playing around with Microsoft Spy++ and noticed that not only does it find the open processes, but can find the individual components running in each process. For example there is this application that allows you to open a window in which there is a textbox for an IP address and textbox for a port. Spy++ can detect these components. Knowing that Spy++ can detect them, is there anyway possible to find them in a separate c# application and go on to MODIFY their contents and otherwise interact with the program? (such as firing a click event on a button)
This is feasible. Try use PInvoke (InterOp) or AutomationElement, or AutomationPeer (for WPF applications) to automate all you wish to do.
Also you might wish to try Inspect and UISpy application as well.
Automation elements/peer is a non-intrusive mechanism to control UI using accessibility framework. One of the weaknesses in windows is its lack of defence against code injection. Put simply:
As a privileged user,
- You can Open and Modify a running Process image
- Make it load your OWN DLL
- Make it run your OWN thread (that potentially listens to commands from your process) and
- allows you to read any bits of memory you want.
Look at detours (http://research.microsoft.com/en-us/projects/detours/) for how to do it with Managed Processes.. Unfortunately, Microsoft removed the inject at runtime features.
Also look at http://msdn.microsoft.com/en-us/magazine/cc163617.aspx for doing things in the managed world (Apps like Snoop utilise that)

Creating a Process in the background that will listen to keyboard

I have a project that runs in the background in a different process, I want it to be able to react to keyboard everywhere, for example I run the project, and afterwards I do other stuff in the computer such as browsing, facebook, watching movies etc.., and every time I press F9 I want my project to show up. Same as how you press a combination of keys to invoke Babylon... I want to implement it in C#, I have no idea how to begin.
You can register a hotkey with the RegisterHotKey API function. You can see an example of its usage from C# here.
I think you need to write a system-wide keyboard hook, check here for details:
https://stackoverflow.com/a/1764434/559144
How do I grab events for all applications? An example system-wide hook.

Why does my console application have command history?

I have written a console application, which is essentially a Console.ReadLine()-Loop. When the application is waiting for input, pressing the up arrow key iterates through all previous lines of input. My application does not contain any code for this feature. What part of Windows provides this? How can I disable it?
I can only image that it's either a feature of the console subsystem or implemented in Console.ReadLine().
Here is some sample code that exhibits the described behavior:
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string input;
do
{
input = System.Console.ReadLine();
} while (input != "exit");
}
}
}
I would like to disable the history feature for now, and re-implement it later using my own code. The current behavior is too limited.
you can change this behaviour of windows programmatically by calling SetConsoleHistoryInfo with a correctly setup CONSOLE_HISTORY_INFO structure... there seems to be no managed class/method so you will have to use DllImport etc.
http://msdn.microsoft.com/en-us/library/ms686031%28v=VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms682077%28v=VS.85%29.aspx
IF need be - several other aspects of the console can be handled in a managed way - see c# console, Console.Clear problem
The history feature is built into the Windows Command shell, it is not a feature of your application. AFAIK there's no way to disable this in your code as it's specific to the Windows Shell Environment (unless there's a setting that can be changed, which there probably is)
You could possibly override the default behavior by using a key listener to get all up arrow keypresses and execute your own code, that way the event doesn't drop down to the shell to handle.
Yes, this is a feature of the console subsystem, not your application. To change it, click the console's control box (top left), properties, options tab: "Command history." The default is 50 items, 4 buffers. Supposedly this can be configured programmatically with DOSKEY from the command line, but a few minutes tinkering didn't lead me anywhere.
ALT+F7 will clear the command history, as will executing the command DOSKEY /reinstall. I tested in Windows 7.
Update: The corresponding Win32 API call is SetConsoleHistoryInfo and the p/invoke signature can be found at http://pinvoke.net/default.aspx/kernel32/SetConsoleHistoryInfo.html
Not tested, but it looks like passing an instance of CONSOLE_HISTORY_INFO to SetConsoleHistoryInfo with buffer size and count set to 1 would give the same control as the console window properties dialogue.
P/Invoke definitions at pinvoke.net
Also note this requires Windows V6 or later (ie. Vista/2008/7/2008R2).

spy keyboard (how to get keyboard digets via C# win forms app)

I want to build a win app using C#...
I want this app to accept letter from the keyboard... and I want that to be done either the text is written via this program or another... its will be much better if I can choose programs I want to spy on...
in another words... I want my program to get every thing presses on the keyboard and everything is being written on firefox,opera,internet explorer witch are running at same time with my program...
You need a global keyboard hook, which will allow your application to listen in on keyboard input events system-wide. You implement this by P/Invoking the SetWindowsHookEx function and specifying the WH_KEYBOARD_LL flag for the idHook parameter. The entire process can get slightly complicated.
But it turns out that you're in luck. Stephen Toub has already written the code for you here on his blog: Low-Level Keyboard Hook in C#. Just drop this into your project and you're in the spy business.
I believe what you are looking for is a keylogger...
if so you can find information on:
http://www.axino.net/tutorial/2009/02/keylogger-in-c-introduction
There's a fairly comprehensive article on this over at Code Project: http://www.codeproject.com/KB/system/KeyLogger.aspx
While that article is based around C++ etc it covers a lot of the technical details you need to know.
There is an example C# project here: http://www.codeproject.com/KB/system/simple_key_log.aspx.

Any high level library or framework to hook all events in one's app in C#?

I know there's hook in Win32 but I don't need to hook the whole system and it's low level.
What I want is something easy like Wordpress framework but for Winform which allows me to hook all events in my own application for example detecting all textbox leave or all forms closing.
Does this exist ? Is it possible technically or only Microsoft can do so in .NET Version X.X ?
Have a look at ManagedSpy. It's an application very similar to Spy++, but for managed applications. It appeared in an MSDN Magazine issue several years ago.
When you run ManagedSpy, you can attach it to a running .Net process. It will reflect on the assemblies and find all kinds of events (there's some filtering ability to only see certain events), then it attaches to them and outputs the sequence of them firing.
There is also source code for ManagedSpy, so you can see how they did things and use those ideas to build what you need.
There is no easy way to do this as I am aware. There are external tools that can help (such as Spy++) but I believe they operate at the Windows message level rather than the .NET event level.
If you really need to do this level of monitoring in your application, you'll need to sign up event handlers on each object you wish to monitor. You could consider writing code that walks the control tree for each Form and signs up their events, so you could run it at startup after the forms are created.

Categories