Results 1 to 4 of 4

Thread: [2.0] VB6 to C#

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517

    [2.0] VB6 to C#

    How does this if statement translate to C# (I dont get the And &H8000 )


    Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer


    Private Sub Timer1_Timer()
    Dim keystate(1) As Integer

    keystate(0) = GetAsyncKeyState(vbKeyControl)
    keystate(1) = GetAsyncKeyState(vbKeyTab)

    If keystate(0) And keystate(1) And &H8000 Then
    'If hotkey is pressed
    End If
    End Sub

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] VB6 to C#

    Code:
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    // ...
    
    [DllImport("user32")] private static extern int GetAsyncKeyState(long vKey);
    
    private void Timer1_Timer()
    {
      int[] keystate = new int[2];
    
      keystate[0] = GetAsyncKeyState(Keys.Control);
      keystate[1] = GetAsyncKeyState(Keys.Tab);
    
      if (keystate[0] & keystate[1] & 0x8000) {
        // hotkey is pressed
      }
    }

    edit: you'll need to modify the signature of the tick event handler as appropriate.
    Last edited by penagate; Sep 4th, 2007 at 11:20 AM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] VB6 to C#

    Remember that VB6 numbers are half the width of .NET numbers, so it should be:
    Code:
    private static extern short GetAsyncKeyState(int vKey);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] VB6 to C#

    Ah, yes. I read VB.NET by mistake.

    Change the array to shorts as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width