473,586 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ 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 "HookCallba ck" is static and for some reason it does work to the point of " MyAKeyWasPresse d(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 974

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

Similar topics

8
7392
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 data is entered here in any field, values are updated on forms C, D, E (not A). I am considering using delegates to fire events from forms A & B. In...
3
3076
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 code. then I can see the data and everything gets updated, until the next time I put a break in the code. Otherwise, I have to put a break on the...
13
1933
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 indicates that the forms timer does not operate on its own thread and will not cause a query to be sent to use the single SqlConnection while another is...
16
2884
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList...
6
2488
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 extremely appropriate (as opposed to other programmatic means or mechanisms) that were NOT related to events and ThreadStarts? Thanks, Jon
5
2756
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 : myEvent -= MyMethodToFire; That's all fine, but when you use visual studio to create events for objects it never creates an unsubscribing...
0
2522
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 version of my code does not work. It seems that the events are not triggering when there is a data on any of the eth or serial ports. Basicaly all...
8
2277
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 after something special. Consider the following example : I'm setting a function with a pointor function, then in a while-true block, I ask for an...
7
2323
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. Is that so?
11
3240
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 my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.