Type Casting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BRawn
    New Member
    • Jul 2010
    • 28

    Type Casting

    I'm trying to cast types in an application a friend asked me to look at for him, but my casts aren't working. I want to cast from EventArgs to KeyEventArgs. Is this possible?

    Here's the just of it:

    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
          MainMenu_Key(sender, e);
    }
    
    private void MainMenu_Key(object sender, EventArgs e)
    {
          KeyEventArgs margs = (KeyEventArgs)e;
          lbl_result.Text = margs.KeyCode.ToString();
          
          if (margs.KeyCode.ToString() == "D1")
          {
              if (tmrTIMER.Enabled == false && AvailableClicks != 0)
          {
              tmrTIMER.Enabled = true;
              NextKeyToPress = 1;
          }
          KeyToPress = NextKeyToPress;
          AvailableClicks -= Clickdeduction;
          Actions += 1;
          KeyPressed = 1;
    
          #region ///colorchange\\\
          if (KeyPressed == KeyToPress)
          { CorrectClick = 1; }
          else { CorrectClick = 0; }
          #endregion
          if (CorrectClick == 1)
          {
              pb_1.Visible = false; }
              pbColors(sender, e);
          }
    }
    I'm getting an InvalidCastExce ption on line 9 (KeyEventArgs margs = (KeyEventArgs)e ;) which I understand but is there another way to cast EventArgs to KeyEventArgs? I'm not too sure about the inheritance of types this way...
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I think you need KeyPressEventAr gs instead of KeyEventArgs.

    I'm also not sure what event you're listening to... I don't see one named Key, so it has to be KeyUp, KeyDown, KeyPress, or KeyPreview... but I'm not sure which it is. Can you please clarify on that?

    Also, you can only cast where the data makes sense and is related. In the actual method signature for the above mentioned events, they actually have a KeyPressEventAr gs parameter. It looks like your friend has renamed that to take the more generic EventArgs object. That's fine, but be aware that while all KeyPressEventAr gs objects are also EventArgs objects, not all EventArgs objects are KeyPressEventAr gs objects.

    Comment

    • BRawn
      New Member
      • Jul 2010
      • 28

      #3
      I tried with KeyPressEventAr gs. KeyEventArgs has a KeyCode property and KeyPressEventAr gs has a KeyChar property. I'm not too sure what the difference between these two properties are but I did try the KeyPressEventAr gs way, and still it didn't work.

      Even though his using the more generic EventArgs object, I think what his trying to achieve is monitor the keys pressed as this application monitors his APM (actions per minute) while playing games.

      The named key in the code I posted in my initial question listens to KeyCode 'D1'. It's a value in the 'Keys' enumerator. I've tried on the main form's KeyUp event but the event never fired even though on form_load, I used the Focus() method to focus on the main form for the KeyUp event to fire, but I didn't have much luck in that department either.

      As all the controls his trying to listen on are pictureBoxes, which don't have any sort or KeyEvents, I told him to use buttons...at least that way he can make it look the same and he'll have KeyEvents to play with :)

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Ah I see what you're looking for. Yea the KeyPressEventAr gs has a key char, which is the actual character the user pressed. KeyDown and KeyUp use KeyEventArgs, which give you a bit more information. However, it should work for you... here's an example.

        Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
        
                this.KeyDown += new KeyEventHandler(Form1_KeyDown);
            }
        
            void Form1_KeyDown(object sender, EventArgs e)
            {
                KeyEventArgs args = (KeyEventArgs)e;
        
                Console.WriteLine("Key down: " + args.KeyCode.ToString());
            }
        }
        If the cast wasn't working, I'd have to guess your friend was listening to a different event (you still haven't posted the code for this). Anyway, it's important to point out that this won't work for what your friend wants to do anyway. Those event handlers are for the control that initiates the event only. In this case, you will only get the event when the form itself is active and in focus.

        In order to record APM, you'll need to globally monitor input, which can be done through Windows API calls. There's an article on CodeProject that will explain this.



        A couple of things to mention though...

        1) This gets pretty close to a key logger. You're basically trapping global keyboard and mouse input, so this kind of thing could be considered a little sketchy. Just a heads up.

        2) I don't know what game your friend wants APM for, but I know SC2 has a built in one and APM is a common metric that people want. Google around and you might be able to find something that exists already. If not, or he/you just want to make it anyway, you're on the right track :)

        Comment

        Working...