Adblock breaks this site

A couple noob questions

Discussion in 'Programming General' started by Blupig, Feb 20, 2008.

  1. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    A couple noob questions

    1. How can I make it so something happens when a key is pressed? In VB6 it's like this (without the declaration):
    Code:
    If GetAsyncKeyState(vbKeynanana) Then
    stuff.happens
    End If
    
    2. What's the sendkeys format in .NET

    I'll be updating this when I have more questions. Thanks everyone!
     
  2. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    A couple noob questions

    Use MSDN.

    For your first question: What I did was save a blank keyboard state, and compare the keyboard state to check the difference, and then you have whether a key is pressed. I got the method from using XNA.

    For your second question, use SendKeys.Send()
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    A couple noob questions

    Isn't it recommended to switch to SendMessage now because Sendkeys was deprecated in Vista?
     
  4. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    A couple noob questions

    I'm just learning .NET - I have no idea how to do that.
     
  5. kharg0

    kharg0 Active Member

    Joined:
    Feb 25, 2007
    Posts:
    131
    Referrals:
    0
    Sythe Gold:
    0
    A couple noob questions

    Code:
    sendkeys.send (keys.YOUR KEY!)
    if u didnt no already jst makin sure

    and if u want it to detect a key press add this to form keypress property..
    Code:
    If e.keycode = Keys.YourKey Then
                'W/E u want it to do wen pressed
            End If
    Code:
     Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.BrowserBack Then
                WebBrowser1.GoBack()
            End If
        End Sub
     
  6. Markizano

    Markizano Active Member
    Banned

    Joined:
    Mar 2, 2006
    Posts:
    100
    Referrals:
    0
    Sythe Gold:
    0
    A couple noob questions

    for this you need a button named Button1
    just keep things simple like you had it:
    Code:
    Friend Class Form1
     Inherits System.Windows.Forms.Form
    
     Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vbKey As Long) As Integer
    
     Private Sub Button1_Click(ByVal Sender As Object, ByVal eArgs As EventArgs) Handles Button1.Click
      If GetAsyncKeyState(32) Then
       MsgBox("The space bar was pressed")
      End If
     End Sub
    
    End Class
    
    However, I experimented with the return values of GetAsyncKeyState.
    For this you need a label named Label1 and a timer named Timer1
    Here an application to this result:
    Code:
    Friend Class Form1
     Inherits System.Windows.Forms.Form
    
     Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vbKey As Long) As Integer
    
    
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      Label1.Text = GetAsyncKeyState(32).ToString
      If Label1.Text = "0" Then
       Label1.Text &= vbNewLine & "The key is released."
      ElseIf Label1.Text = "1" Then
       Label1.Text &= vbNewLine & "The key was pressed."
      ElseIf Label1.Text = "-32767" Then
       Label1.Text &= vbNewLine & "The key is down."
      ElseIf Label1.Text = "-32768" Then
       Label1.Text &= vbNewLine & "The key has been pressed within the last 1000 milliseconds."
      End If
     End Sub
    
    End Class
    
    That's how you detect a keypress in VB.NET, but it works best with a timer with an interval from 300 to 500 ms.

    And, just to verify, they are correct when they say that
    Code:
    SendKeys.Send(ByVal Keys As String, ByVal Control As System.Windows.Forms.Control, ByVal Wait As Boolean)
    
    is the correct method to use when sending text to the open application that has input focus.
     
  7. skate4lifee

    skate4lifee Active Member

    Joined:
    Jul 22, 2007
    Posts:
    108
    Referrals:
    1
    Sythe Gold:
    0
    A couple noob questions

    i haven't used visual basic in a while, but i'm pretty sure you can use GetAsyncKeyState. go to the form's key down property (not sure) and add this code.

    Code:
    if GetAsyncKeyState(keys.F1) then
    messagebox.show("hello")
    end if
     
  8. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    A couple noob questions

    I dunno, is it?
     
< QUICK QUESTION-need help saveing | Looking For Visual C++ >


 
 
Adblock breaks this site