 |
|
 |
Hello, I'm using your great hook class, and it's been responding great, until I needed to use Settings in Visual Studio! Basically, I allow the user to save his settings; the settings of the modified key functions. Just adding this line in the code generates an annoying error:
if (Properties.Settings.Default.DisableAll == false) { }
The error I'm getting is:
A callback was made on a garbage collected delegate of type 'key preview!Utilities.globalKeyboardHook+keyboardHookProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
This is really stopping me from further progress, and I am completely unable to do anything about it, even MSDN experts are not capable to providing solutions!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
 | Great  Ravenet | 8:24 9 Oct '09 |
|
|
 |
|
 |
Great Code, thanks!
I was trying to capture Alt+Q. I was able to get "Alt" through Control.ModifierKeys. However, when casting lParam.vkCode to (Keys), I constantly got "MButton | Space | F17", and vkCode was always 164. (I was expecting to get "Q")
I tried with other key combinations, stil getting the same thing. Does anyone knoow what could have caused this and how to resolve it?
Thanks a lot!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
As another user has posted below, at random keystroke intervals, it stops capturing them? Any idea as to why?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
i havre the same problem, after 7 times pressing my hotkey, the proggramm just stops hooking the key.... any ideas why?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hi
i'm trying to get it to work in wpf. i have only one problem that is the keys..
using system.windows.input.key
the problem is in here: Key key = (Key)lParam.vkCode;
i get a code that is for windows form keys.. what do i have to change, to work with the right key in wpf..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
I added your class to my wpf project but I get an exception with the following message:
A callback was made on a garbage collected delegate of type 'BingIt!Utilities.globalKeyboardHook+keyboardHookProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
and then nullreferenceexception...Any ideas?
Thanks for sharing your usefull code 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
So i cant make it unhook the keyboard, and when i press a button only the program registers it . Any way to fix this?
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Utilities; using System.IO;
namespace KeyLog { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
globalKeyboardHook g_hk = new globalKeyboardHook();
bool load = false;
private void Form1_Load(object sender, EventArgs e) {
} void g_hk_KeyDown(object sender, KeyEventArgs e) { log_view.Items.Add("Down\t" + e.KeyCode.ToString()); e.Handled = true; } private void btn_load_Click(object sender, EventArgs e) { if (load == false) { g_hk.hook(); foreach (object key in Enum.GetValues(typeof(Keys))) g_hk.HookedKeys.Add((Keys)key);
g_hk.KeyDown += new KeyEventHandler(g_hk_KeyDown); load = true; } } private void btn_dump_Click(object sender, EventArgs e) { string time = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); string dump = " "; for (int i = 0; log_view.Items.Count > i; i++) { log_view.SelectedIndex = i; dump += log_view.Text.ToString() + Environment.NewLine; } StreamWriter d_ump = new StreamWriter(@Application.StartupPath + "\\" + time + ".txt"); d_ump.Write(dump); d_ump.Close(); } private void button1_Click(object sender, EventArgs e) { g_hk.unhook(); } } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Im getting the following error while trying to use this class in my app
A callback was made on a garbage collected delegate of type 'TestFlashCommunication!Utilities.globalKeyboardHook+keyboardHookProc::Invoke'. This may cause application crashes, corruption and data loss.
How can I prevent this from happening?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
This is how I modified the code:
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); private keyboardHookProc delegateReference; // Needed to ensure the GC will not remove the delegate . . . public void hook() { delegateReference = hookProc; IntPtr hInstance = LoadLibrary("User32"); hhook = SetWindowsHookEx(WH_KEYBOARD_LL, delegateReference, hInstance, 0); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I fell into the same problem, apparently it's really confusing. The work around has been explained in this thread before by another guy:
Replace the hook process with a delegate that you've assigned a method. This will fix the problem.
regards, Kenny Liew
Excerpt from the past posts:
keyboardHookProc khp;
public GlobalKeyboardHook() { //assign a method to the delegate, which is hookProc in the example khp = new keyboardHookProc(hookProc); hook(); }
//And I modified this function: public void hook() { IntPtr hInstance = LoadLibrary("User32"); hhook = SetWindowsHookEx(WH_KEYBOARD_LL, khp, hInstance, 0); //notice that the hookProc has been replaced by khp instead }
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
I want to use all they keys to be going through my keyboard hook application, and not just A and B. How can I do that? (Except some key combination like Ctrl. Shift Esc. etc. )
Thanks !!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Add them to the HookedKeys collection, you can use the Enum class to get all of the values of the Keys enumeration
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I did it by adding the following:
private bool _hookAll = false; and
public bool HookAllKeys { get { return _hookAll; } set { _hookAll = value; } }
and then modify the hookProc function as such:
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; }
Hope this helps
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi, I used your example to capture keystrokes. It captures them globally without any problem, but only upto a certain number of key strokes(which is random). For example, sometimes when I have pressed 280 keys it stops capturing. Sometimes 236, 182 rarely, but even 8 !! I know it might not be related to these numbers at all. (I'm running Windows Vista) but what makes it stop capturing key strokes?
Thanks !
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
|
 |