Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 帮助修改键盘记录器应用程序_C# - Fatal编程技术网

C# 帮助修改键盘记录器应用程序

C# 帮助修改键盘记录器应用程序,c#,C#,我从互联网上得到这个应用程序,我想添加一些修改。不幸的是,我不知道该怎么办。此应用程序是一个简单的键盘记录器,它将日志保存在文本文件中 *在键盘记录发生后阅读文本文件后,我注意到它的单词都是大写的,对于空格或回车等标点符号,使用了空格和回车 任何人都可以修改代码来保存角色的确切大小写吗?我不能完全理解代码。。。。谢谢 表格1.cs globalKeyboardHook.cs 重要的部分是这一行: SW.Write(e.KeyCode); 请注意,e.KeyCode的类型为KeyCode,它是一

我从互联网上得到这个应用程序,我想添加一些修改。不幸的是,我不知道该怎么办。此应用程序是一个简单的键盘记录器,它将日志保存在文本文件中

*在键盘记录发生后阅读文本文件后,我注意到它的单词都是大写的,对于空格或回车等标点符号,使用了空格和回车

任何人都可以修改代码来保存角色的确切大小写吗?我不能完全理解代码。。。。谢谢

表格1.cs

globalKeyboardHook.cs


重要的部分是这一行:

SW.Write(e.KeyCode);
请注意,e.KeyCode的类型为KeyCode,它是一个枚举。此枚举具有诸如“A”键的A、空格键的空格等值。使用此值调用SW.Write将枚举值转换为包含其名称的字符串,并将其写入文件


看起来您的全局键盘挂钩没有提供任何功能来将此键码转换为实际字符。实现这一点非常困难:键入的字符不仅取决于同时按下的其他键(例如Shift或AltGr),还取决于当前的键盘布局。用户可以安装多个不同的键盘布局,并随时在它们之间切换。

键盘记录器正在做的就是:记录击键。在Form1.Form1_Load中,每次按下一个键时,它都将gkh_keyDown注册为处理程序。重要的是,当一个键被释放时,没有处理程序——没有设置要监视事件gkh_keyUp。但是根GlobalKeyboardHook库确实提供了这些事件

您需要编写一个新函数(可能是gkh_keyUp)来处理keyUp事件。这是唯一能知道何时有人松开换档钥匙的方法

如果您只关心SHIFT+字母,并且在释放Ctrl或Alt时,您需要执行以下操作:

为当前是否按下SHIFT添加布尔值。 每当发现SHIFT是按键按下事件中的按键时,设置该标志。只要钥匙在钥匙上,就清除它。 只要键字符串是空格,就改为检测空格。 如果未设置SHIFT标志,则在将密钥字母写入文件时使用String.ToLower;如果未设置,请保持大写状态。 如果向上键接收到Ctrl或Alt,则将其设为print-Ctrl或-Alt以表示正在释放的键。除了更改移位标志外,向上键处理程序可能应该为空。
这不是一个很大的重写,但是在这个评论框中重写它感觉有点太多了。与此相关的是,您根本不需要更改GlobalKeyboardHook.cs,您应该阅读关于事件、事件处理和委托的C参考,以了解Form1_Load中发生了什么,如果您不确定如何注册密钥更新事件。

邪恶应用程序+代码墙==投票关闭。+完全通用且不具信息性的主题行…我建议您自己研究此应用程序,然后询问您不了解的应用程序特定部分的问题。这是一个问答网站,不是一个“heres some code”,改为“为我工作”网站我考虑过将标题改为“我想写一个键盘记录程序来监视人们,但我想不出这是我从互联网下载的代码”,但我觉得这有点太卑鄙了。@Tim不要认为他有恶意。。虽然这是可能的。我尝试使用KeyChar,但该事件没有这样的属性。我们应该如何转换它呢?如果我们只关心大小写,我们只需要跟踪shift键是否被按住。将keycode转换为case的最简单方法是间接执行:e.keycode.ToString.ToLower。无论如何,ToString都是由Write隐式完成的。@Kistaro,如果键实际上是大写的,那怎么办?您的代码产生了错误的结果。@yonan,代码挂接到了错误的事件中。看看你最近的另一个问题。按键是一个你想看的事件。虽然答案已经在您的代码示例中,但我不会告诉您如何使用它。非常感谢大家:我有一些想法。你所描述的不是一个大的重写,但那是因为它没有考虑像Shift+1=!或AltGr+e=é或西里尔键盘或……谢谢。。。。。我会试试你的想法。这不是一个完整的解决方案,但只是一个开始。这里有各种各样的模糊案例需要处理,但对所有这些案例进行详细说明可能不是最好的做法。在这里,明显的指向不明显的方向。好的一点,尤其是关于数字行;这需要实现一个查找表。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Utilities {
    /// <summary>
    /// A class that manages a global low level keyboard hook
    /// </summary>
    class globalKeyboardHook {
        #region Constant, Structure and Delegate Definitions
        /// <summary>
        /// defines the callback type for the hook
        /// </summary>
        public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);

        public struct keyboardHookStruct {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        public bool _hookAll = false;
        public bool HookAllKeys
        {
            get
            {
                return _hookAll;
            }
            set
            {
                _hookAll = value;
            }
        }
        const int WH_KEYBOARD_LL = 13;
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_SYSKEYDOWN = 0x104;
        const int WM_SYSKEYUP = 0x105;
        #endregion

        #region Instance Variables
        /// <summary>
        /// The collections of keys to watch for
        /// </summary>
        public List<Keys> HookedKeys = new List<Keys>();
        /// <summary>
        /// Handle to the hook, need this to unhook and call the next hook
        /// </summary>
        IntPtr hhook = IntPtr.Zero;
        keyboardHookProc khp; 
        #endregion

        #region Events
        /// <summary>
        /// Occurs when one of the hooked keys is pressed
        /// </summary>
        public event KeyEventHandler KeyDown;
        /// <summary>
        /// Occurs when one of the hooked keys is released
        /// </summary>
        public event KeyEventHandler KeyUp;
        #endregion

        #region Constructors and Destructors
        /// <summary>
        /// Initializes a new instance of the <see cref="globalKeyboardHook"/> class and installs the keyboard hook.
        /// </summary>
        public globalKeyboardHook()
        {
            khp = new keyboardHookProc(hookProc);
            hook();
        }
        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="globalKeyboardHook"/> is reclaimed by garbage collection and uninstalls the keyboard hook.
        /// </summary>
        ~globalKeyboardHook() {
            unhook();
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Installs the global hook
        /// </summary>
        public void hook()
        {
            IntPtr hInstance = LoadLibrary("User32");
            hhook = SetWindowsHookEx(WH_KEYBOARD_LL, khp, hInstance, 0);
        } 

        /// <summary>
        /// Uninstalls the global hook
        /// </summary>
        public void unhook() {
            UnhookWindowsHookEx(hhook);
        }

        /// <summary>
        /// The callback for the keyboard hook
        /// </summary>
        /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
        /// <param name="wParam">The event type</param>
        /// <param name="lParam">The keyhook event information</param>
        /// <returns></returns>
        public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) {
            if (code >= 0)
            {
                Keys key = (Keys)lParam.vkCode;
                if (_hookAll ? true : HookedKeys.Contains(key))
                {
                    KeyEventArgs kea = new KeyEventArgs(key);
                    if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
                    {
                        KeyDown(this, kea);
                    }
                    else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
                    {
                        KeyUp(this, kea);
                    }
                    if (kea.Handled)
                        return 1;
                }
            }
            return CallNextHookEx(hhook, code, wParam, ref lParam);
        }

        #endregion

        #region DLL imports
        /// <summary>
        /// Sets the windows hook, do the desired event, one of hInstance or threadId must be non-null
        /// </summary>
        /// <param name="idHook">The id of the event you want to hook</param>
        /// <param name="callback">The callback.</param>
        /// <param name="hInstance">The handle you want to attach the event to, can be null</param>
        /// <param name="threadId">The thread you want to attach the event to, can be null</param>
        /// <returns>a handle to the desired hook</returns>
        [DllImport("user32.dll")]
        static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);

        /// <summary>
        /// Unhooks the windows hook.
        /// </summary>
        /// <param name="hInstance">The hook handle that was returned from SetWindowsHookEx</param>
        /// <returns>True if successful, false otherwise</returns>
        [DllImport("user32.dll")]
        static extern bool UnhookWindowsHookEx(IntPtr hInstance);

        /// <summary>
        /// Calls the next hook.
        /// </summary>
        /// <param name="idHook">The hook id</param>
        /// <param name="nCode">The hook code</param>
        /// <param name="wParam">The wparam.</param>
        /// <param name="lParam">The lparam.</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam);

        /// <summary>
        /// Loads the library.
        /// </summary>
        /// <param name="lpFileName">Name of the library</param>
        /// <returns>A handle to the library</returns>
        [DllImport("kernel32.dll")]
        static extern IntPtr LoadLibrary(string lpFileName);
        #endregion
    }
}
SW.Write(e.KeyCode);