fixed comment into code
This commit is contained in:
@@ -3,22 +3,22 @@ using System.Runtime.InteropServices;
|
||||
namespace CursorLang.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// Системный перехватчик клавиатуры (WH_KEYBOARD_LL): видит нажатия во всех
|
||||
/// приложениях и умеет не пропускать их дальше.
|
||||
/// A system keyboard hook (WH_KEYBOARD_LL): sees key presses in every application
|
||||
/// and can keep them from going any further.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Ставится только низкоуровневый хук: обычный WH_KEYBOARD требует внедрения
|
||||
/// DLL в чужие процессы, что для managed-кода невозможно. Обратный вызов
|
||||
/// приходит на тот поток, который поставил хук, и поток обязан крутить цикл
|
||||
/// сообщений — отсюда требование ставить хук из потока пользовательского
|
||||
/// интерфейса. Возврат управления затягивать нельзя: по истечении системного
|
||||
/// тайм-аута Windows молча снимает хук.
|
||||
/// Only the low-level hook is installed: a regular WH_KEYBOARD requires injecting a
|
||||
/// DLL into other processes, which is impossible for managed code. The callback
|
||||
/// arrives on the thread that installed the hook, and that thread must pump a message
|
||||
/// loop — hence the requirement to install the hook from the user interface thread.
|
||||
/// Returning control must not be delayed: once the system timeout expires, Windows
|
||||
/// silently removes the hook.
|
||||
/// </remarks>
|
||||
internal sealed class LowLevelKeyboardHook : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Обработчик события клавиши. Возвращает <c>true</c>, если событие нужно
|
||||
/// проглотить — тогда приложение переднего плана его не увидит.
|
||||
/// A key event handler. Returns <c>true</c> when the event must be swallowed —
|
||||
/// then the foreground application will not see it.
|
||||
/// </summary>
|
||||
internal delegate bool KeyFilter(int virtualKey, bool isKeyDown);
|
||||
|
||||
@@ -30,14 +30,14 @@ internal sealed class LowLevelKeyboardHook : IDisposable
|
||||
private const int WmSysKeyDown = 0x0104;
|
||||
private const int WmSysKeyUp = 0x0105;
|
||||
|
||||
/// <summary>Событие пришло не от живого нажатия, а из SendInput.</summary>
|
||||
/// <summary>The event came from SendInput rather than from a real key press.</summary>
|
||||
private const uint LowLevelKeyHookFlagInjected = 0x10;
|
||||
|
||||
private readonly KeyFilter _filter;
|
||||
|
||||
// Делегат живёт в поле не для удобства: ссылку на него держит только Win32,
|
||||
// о которой сборщик мусора не знает, и без поля хук перестаёт работать
|
||||
// через случайное время
|
||||
// The delegate lives in a field not for convenience: the only reference to it is
|
||||
// held by Win32, which the garbage collector knows nothing about, and without the
|
||||
// field the hook stops working after a random amount of time
|
||||
private readonly HookProc _callback;
|
||||
|
||||
private IntPtr _handle;
|
||||
@@ -53,7 +53,7 @@ internal sealed class LowLevelKeyboardHook : IDisposable
|
||||
internal bool IsInstalled => _handle != IntPtr.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// Ставит хук. Возвращает <c>false</c>, если система отказала.
|
||||
/// Installs the hook. Returns <c>false</c> when the system refuses.
|
||||
/// </summary>
|
||||
internal bool Install()
|
||||
{
|
||||
@@ -104,14 +104,14 @@ internal sealed class LowLevelKeyboardHook : IDisposable
|
||||
|
||||
var data = Marshal.PtrToStructure<KeyboardHookData>(lParam);
|
||||
|
||||
// Синтетический ввод присылают экранные клавиатуры, программы
|
||||
// автозамены и средства автоматизации: подменять их работу не наше дело
|
||||
// Synthetic input comes from on-screen keyboards, text expanders and
|
||||
// automation tools: overriding what they do is none of our business
|
||||
bool injected = (data.flags & LowLevelKeyHookFlagInjected) != 0;
|
||||
|
||||
if ((isKeyDown || isKeyUp) && !injected && _filter((int)data.vkCode, isKeyDown))
|
||||
{
|
||||
// Ненулевой результат вместо CallNextHookEx обрывает цепочку: событие
|
||||
// не дойдёт ни до приложения, ни до обработчика регистра в Windows
|
||||
// A non-zero result instead of CallNextHookEx breaks the chain: the event
|
||||
// will reach neither the application nor the Windows caps-lock handler
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user