473,466 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Events with static functions

2 New Member
I have a class that is able to detect a global key press. However, I don't seem to be able to create a working event that notifies the Form1 of a global keypress.

The function "HookCallback" is static and for some reason it does work to the point of " MyAKeyWasPressed(int vkKeyCode)" displaying it's 2 messageboxes but the function in Form1 doesn't get fired and displays no messagebox.

What do I do wrong? I'm breaking my head on it for days now. Thanks in advance.

The code in Form1:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.         InterceptKeys Hook = new InterceptKeys();
  3.  
  4.         private void Form1_Load(object sender, EventArgs e)
  5.         {
  6.             Hook.MyKeyPressed += new MyOnKeyPressHandler(MyAKeyWasPressed);
  7.          }
  8.  
  9.         private void MyAKeyWasPressed(int vkKeyCode)
  10.         {
  11.             MessageBox.Show("Form1 succesfully received an event from the class!");
  12.             MessageBox.Show(((Keys)vkKeyCode).ToString());
  13.         }
  14.  

The code in the class:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. namespace MK
  3. {
  4.     public delegate void MyOnKeyPressHandler(int KeyCode);
  5.     class InterceptKeys
  6. {
  7.         public event MyOnKeyPressHandler MyKeyPressed;
  8. ...
  9.         public InterceptKeys()  // constructor
  10.         {
  11.             MyKeyPressed = new MyOnKeyPressHandler(OnKeyPressed);
  12.         }
  13.         private static void OnKeyPressed(int vkKeyCode)
  14.         {
  15.             MessageBox.Show("The function OnKeyPressed in the class worked! The key I found was: "+((Keys)vkKeyCode).ToString());
  16.         }
  17.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  18.         {
  19.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  20.             {
  21.                 int vkCode = Marshal.ReadInt32(lParam);
  22.                 OnKeyPressed(vkCode);
  23.             }
  24.  
  25.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  26.         }
  27.  
  28. ...
  29.  
  30. }
  31.  
Nov 29 '08 #1
0 969

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: STom | last post by:
I have a C# Winforms app that has 5 Winforms, lets say A through E. A: Data entry. When data is entered here in any field, values are updated on forms C, D, E.(Not B) B: Data entry form. When...
3
by: Marco Martin | last post by:
Hi, I'm writing an application that reads a serial port and 1)updates a list and sends data to a graph. When I run the application, nothing happens unless I put a break point anywhere in my...
13
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
6
by: Jon Davis | last post by:
I've used delegates fairly heavily for several years in C# for event handling and for starting threads. Does anyone have any real-world scenarios where delegates were both extremely useful and...
5
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so...
0
by: guyarkam | last post by:
I have the following code in which I use events to read on eth or serial port when data commes in. The ASCHFProtLib library is a third party library that I have to use as it is. Unfortunately this...
8
by: timor.super | last post by:
Hi group, I would like to simulate something that can be quite similar to "Events". I would like that a member function of a class can be "setted" as an Event and that this method can be called...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.