Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:39:41+00:00 2026-05-31T05:39:41+00:00

This seems to be a pretty basic concept that I don’t understand. In writing

  • 0

This seems to be a pretty basic concept that I don’t understand.

In writing a .NET wrapper for a keyboard driver, I’m broadcasting an event for each key pressed, like so (simplified code below):

// The event handler applications can subscribe to on each key press
public event EventHandler<KeyPressedEventArgs> OnKeyPressed;
// I believe this is the only instance that exists, and we just keep passing this around
Stroke stroke = new Stroke();

private void DriverCallback(ref Stroke stroke...)
{
    if (OnKeyPressed != null)
    {
        // Give the subscriber a chance to process/modify the keystroke
        OnKeyPressed(this, new KeyPressedEventArgs(ref stroke) );
    }

    // Forward the keystroke to the OS
    InterceptionDriver.Send(context, device, ref stroke, 1);
}

Stroke is a struct which contains the scancode for the pressed key, and a state.

In the above code, since I’m passing the value-type structure by reference, any changes made to the struct will be ‘remembered’ when passed to the OS (so that pressed keys may be intercepted and modified). So that’s fine.

But how do I let subscribers to my OnKeyPressed event modify the struct Stroke?

The following doesn’t work:

public class KeyPressedEventArgs : EventArgs
{
    // I thought making it a nullable type might also make it a reference type..?
    public Stroke? stroke;

    public KeyPressedEventArgs(ref Stroke stroke)
    {
        this.stroke = stroke;
    }
}

// Other application modifying the keystroke

void interceptor_OnKeyPressed(object sender, KeyPressedEventArgs e)
{
    if (e.stroke.Value.Key.Code == 0x3f) // if pressed key is F5
    {
        // Doesn't really modify the struct I want because it's a value-type copy?
        e.stroke.Value.Key.Code = 0x3c; // change the key to F2
    }
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:39:42+00:00Added an answer on May 31, 2026 at 5:39 am

    Something like this may do the trick:

    if (OnKeyPressed != null)     
    {         
      // Give the subscriber a chance to process/modify the keystroke         
      var args = new KeyPressedEventArgs(stroke);
      OnKeyPressed(this, args);     
      stroke = args.Stroke;
    } 
    

    Give your subscriber a copy and then copy it back to your local value once they’re done with it.

    Alternatively, can you create your own class representing a keystroke and pass that to the subscriber?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a pretty basic question but I unfortunately don't know the answer
So this seems pretty basic but I can't get it to work. I have
This seems pretty basic, but I can't find the best method to do this...
This C++ question seems to be pretty basic and general but still I want
This seems like a pretty basic thing to do but although I've been using
This seems like a pretty obvious, basic thing to expect from the Graph API
this is a pretty basic question but I can't seem to get it right.
This seems like a pretty softball question, but I always have a hard time
This seems like it should be pretty straight forward, but I'm apparently confused. I
This feels like it should be pretty simple, but not much seems to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.