VB6 using keydown

Discussion in 'Programmer's Corner' started by lloydi12345, Jan 4, 2011.

  1. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Hi, I'm creating a sample program in VB6. It is consist only of one textbox and one command button. If I'll press the command button it will output "f" on the textbox as stated on my code below. Can you teach me how to make the command button be "pressed" without using images or pictures using only keyboard keys. For example using the cursor key UP, when I press it, the command button will be "pressed" (it should look like it is being pressed also) and when I release the cursor key Up again then the command button will be "unpressed". I hope you understand what I mean. Here's my first code:

    Code ( (Unknown Language)):
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. On Error Resume Next
    3. Select Case KeyCode
    4. Case vbKeyUp
    5.     text1.text = "f"   'output "f" on textbox
    6. End Select
    7. End Sub
    8.  
    9. Private Sub Command1_Click()
    10. text1.text = "f"
    11. End Sub
    12.  
    Thanks in advance.
     
    Last edited: Jan 4, 2011
  2. edgetrigger

    Member

    Dec 19, 2010
    133
    19
    do you want the same button to appear depressed and hold that position till another event triggers? Sorry didn't get you.
     
  3. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Thanks for the reply edgetrigger,
    Since I have only one textbox and one command button in the program, yes. I want the only one command button to be pressed and unpressed when I'm pressing the vbkey.
     
  4. jOmega

    New Member

    Mar 22, 2011
    1
    0
    Lloyd,

    To make it work, there are a couple of considerations.
    1 - Form1 Properties : KeyPreview needs to be set to TRUE. (default is FALSE) This enables the Up Arrow key being pressed to be detected. When set to FALSE, the arrow keys will move things but you will not be able to detect their being pressed.

    You can either set that property to TRUE in the design environment or, you can make the change in the Form Load procedure as follows:

    Private Sub Form_Load()
    Form1.KeyPreview = True
    End Sub

    With that accomplished, now your code will place the letter f in the Text Box when the Up Arrow key is depressed.

    2. Next, you said that you wanted the Command1 button to appear to be depressed when the Up Arrow key is depressed.... and to remain depressed until the Up Arrow key is released. This is accomplished by changing the Command1 Default property from False to True and then back to False. The code below accomplishes that.

    Private Sub Form_Load()

    Form1.KeyPreview = True

    End Sub
    ---------------------------------------------------------------
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    On Error Resume Next

    Select Case KeyCode
    Case vbKeyUp
    End Select

    Command1.Default = True

    Text1.Text = "f"

    End Sub
    --------------------------------------------------------------
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

    On Error Resume Next

    Select Case KeyCode
    Case vbKeyUp
    End Select

    Command1.Default = False

    End Sub
    -------------------------------------------------------------
    Private Sub Command1_Click()

    Text1.Text = "f"

    End Sub
     
Loading...