3 Replies - 842 Views - Last Post: 17 August 2012 - 04:50 AM Rate Topic: -----

#1 djaraojo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-July 12

press and hold a button with keyascii but will execute the code once

Posted 17 August 2012 - 12:16 AM

Im just a beginner in VB6 and in this site, im not sure if there's a thread about this issue in dreamincode so please tell me if it already exist, thanks.

Im sorry if the title made you confused :(, I am working on a program with some push buttons so I needed to use keyascii, the problem is, when I press and hold down a button it execute the code continuously until I stop holding down that button.. I want the code to be execute only once even the push button is pressed and hold.

my current code:
*Press and hold the button
*Execute the code repeatedly until I stop pressing that button

i need my code to be:
*Press and hold the button
*Execute the code ONCE

Here's the code I use:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 113 'q +2M
    If Label8.Caption = "p1" Then
        If Command1(0).Enabled = True Then
            Call Command1_Click(0)
	'tried this code (keyascii = 0) for this LINE but it doesnt work
	'also tried (exit sub) but still doesnt solve the problem :(/>
	'anyone can suggest good solution for this problem
        End If
    ElseIf Label8.Caption = "p2" Then
        If Command1(47).Enabled = True Then
            Call Command1_Click(47)
        End If
    ElseIf Label8.Caption = "p3" Then
        If Command1(119).Enabled = True Then
            Call Command1_Click(119)
        End If
    ElseIf Label8.Caption = "p4" Then
        If Command1(95).Enabled = True Then
            Call Command1_Click(95)
        End If
    ElseIf Label8.Caption = "p5" Then
        If Command1(71).Enabled = True Then
            Call Command1_Click(71)
        End If
    End If
End Select
End Sub



Any suggestions or corrections will be much appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: press and hold a button with keyascii but will execute the code once

#2 maj3091  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: press and hold a button with keyascii but will execute the code once

Posted 17 August 2012 - 01:09 AM

Have a look at using the KeyUp event, it might work better for you.
Was This Post Helpful? 0
  • +
  • -

#3 djaraojo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-July 12

Re: press and hold a button with keyascii but will execute the code once

Posted 17 August 2012 - 04:23 AM

View Postmaj3091, on 17 August 2012 - 01:09 AM, said:

Have a look at using the KeyUp event, it might work better for you.


Keyup event? O.o can you explain it more further?

because the problem is when the button is pressed down :/
Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: press and hold a button with keyascii but will execute the code once

Posted 17 August 2012 - 04:50 AM

Try setting a flag in your KeyDown event around your call. You use can use the KeyUp to reset the flag, then ensures the code is triggered once.

dim blnExecuting as boolean

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

  if not blnExecuting then
    'Execute your code here
    blnExecuting =true
  end if

End sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    blnExecuting = False
End Sub

This post has been edited by maj3091: 17 August 2012 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1