N
If your window loses focus, but you still want it to respond to the pressure of the keyboard, then the solution is to have a global hook on the keyboard (poss a mouse, by the way, too).In general, when the application is launched, you register your keyboard processor, process your compression, then transfer the events further (may be in addition to your and other cocks in the system) and correctly remove your huk when the application is completed.The topic of work with the keyboards came up here, for example, here. https://ru.stackoverflow.com/a/130397/222168 which refers to articles and the implementation of the Hukov.Or... https://ru.stackoverflow.com/a/581155/222168 (I won't dig him up here).There you go. http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C with an example in English.And there's more. https://blogs.msdn.microsoft.com/dmandreev/2010/11/29/windows-c/ Russian in Miccrosoft blog, exemplified.
The article can move or disappear over time, because the code is where I'm gonna let myself sell here. There is an example of the designation and removal of the Hook, and the processing of the combination of keys, in particular, is being processed. Alt + Tab♪According to the author,An example of this will work on both the 32nd and the 64th battle Windows. The programme shall be pumped to the cooling console in any annex and shall lock the alt+Tab. As you understand, the interception of the keyboard is at the level of all applications.So...using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace HookDemoApp
{
internal class HookDemoHelper
{
private const int WH_KEYBOARD_LL = 13;
private LowLevelKeyboardProcDelegate m_callback;
private IntPtr m_hHook;
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(
int idHook,
LowLevelKeyboardProcDelegate lpfn,
IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("Kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(IntPtr lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr CallNextHookEx(
IntPtr hhk,
int nCode, IntPtr wParam, IntPtr lParam);
private IntPtr LowLevelKeyboardHookProc(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0)
{
return CallNextHookEx(m_hHook, nCode, wParam, lParam);
}
else
{
var khs = (KeyboardHookStruct)
Marshal.PtrToStructure(lParam,
typeof (KeyboardHookStruct));
Debug.Print("Hook: Code: {0}, WParam: {1},{2},{3},{4} ",
nCode, wParam, lParam,
khs.VirtualKeyCode,
khs.ScanCode, khs.Flags, khs.Time);
Debug.Print(khs.VirtualKeyCode.ToString());
if (khs.VirtualKeyCode == 9 &&
wParam.ToInt32() == 260 &&
khs.ScanCode == 15) //alt+tab
{
System.Console.WriteLine("Alt+Tab pressed!");
IntPtr val=new IntPtr(1);
return val;
}
else
{
return CallNextHookEx(m_hHook, nCode, wParam, lParam);
}
}
}
[StructLayout(LayoutKind.Sequential)]
private struct KeyboardHookStruct
{
public readonly int VirtualKeyCode;
public readonly int ScanCode;
public readonly int Flags;
public readonly int Time;
public readonly IntPtr ExtraInfo;
}
private delegate IntPtr LowLevelKeyboardProcDelegate(
int nCode, IntPtr wParam, IntPtr lParam);
public void SetHook()
{
m_callback =LowLevelKeyboardHookProc;
m_hHook =SetWindowsHookEx(WH_KEYBOARD_LL,
m_callback,
GetModuleHandle(IntPtr.Zero),0);
}
public void Unhook()
{
UnhookWindowsHookEx(m_hHook);
}
}
♪Everything. ♪