Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 - Key depressed

Status
Not open for further replies.
Jun 30, 2003
196
GB
I was wondering if there is a way to attach to event handling procedures to one control. For instance when the button is clicked it performs one function and when it is held down for longer than two seconds it performs another function.

Is there a way in VB6 to acheive this kind of functionality.

Thanks any advice welcome
 
Some of the smart guys here should have a much better idea but my thoughts are to use a timer control

set the interval as required

reset the timer to 0 on the key down to start the timer,

reset the timer to 0 on key up to stop it

if the timer event occurs do whatever else do nothing.

crude, but it should work



Terry (cyberbiker)
 
so doing it that way should allow me to create two different event based upon one command button depending on how long the key was depressed for, is that correct?

thanks for the advice by the way
 
Here is some noddy code that works:

Put Command1 on Form1, and paste this code into the form:

'==================================================

Option Explicit
Dim start_t As Date
Dim end_t As Date

Private Sub Command1_Click()
'compare to button up time
end_t = Now()
'2 seconds?
If (end_t - start_t) * 100000 < 2 Then
MsgBox &quot;Click code&quot;
End If
End Sub

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'record time the button went down
start_t = Now()
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'compare with button up time
end_t = Now()
'2 seconds?
If (end_t - start_t) * 100000 < 2 Then

Else
MsgBox &quot;Long press code&quot;
End If
End Sub
 
Jeff your idea is better than mine I think. At least it looks more efficient.

I would still write a couple of subs calling the same code from keydown/mousedown events and keyup/mouseup events with the forms keypreview property set to true.





Terry (cyberbiker)
 
Why not just use it with a Control, Alt or Shift key combination?

If Button = 1 And Shift = 1 Then
 
I cant use the shift method because i am porting the appkication to a m obile phone, which does not support shift keys
 
a ha i was looking at one of my old posts as i am now in the stages of implementing a function that when the key is held down a different function occurs to when it is just pressed, and i have just noticed that you posted some code to me Jeff. I know i am getting annoying but please bare with me as i only have a couple more hurdles left.

The code you supplied above i have a couple of questions about. I think i understand what it does but i was wondering are mouse down and mouse up standard event in VB6 or could these be called anything.

 
If you look at the top of a VB code window you will find two dropdowns. The left one lists the Controls on the form or in the code module. When you select any of those controls, then the right hand dropdown shows the list of events for that control

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Of so if i create these two subs(events) mouse down and mouse up will they be triggered every time the mouse is clicked and the mouse is releases respectively?

 
Provided that the control raises these events, yes.

For answers to this level of question, it's often quicker to use VBHelp, or to highlight the keyword in your vb code and hit {F1}

Have you read faq222-2244 in detail yet? It gives quite a few pointers to getting the best from this forum

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
yes perhaps you are right but misconfiguration of my VB6 i dont have the help files on this machine.

You say as long as the control raises it so say i have nine command buttons each one would have to have the bove code in to raise the mouse up and mouse down events?

 
> but misconfiguration of my VB6 i dont have the help files on this machine.

You surely have the VB cds, and on them are the help files.
Also, you can just test the suggestions very easily yourself to see if they produce the desired results.
 
I have to agree with LostInCode
You are struggling to write your program because you don't have the help files, and you seem reluctant to experiment.

Looking back at the code I posted above, this works for one button, but since the start and end times are global variables, it won't work without modification if you want nine buttons to do the same sort of thing.
For that you need 9 start times.

If I was doing this, I'd be looking for some easy place to store the start time for each button, and I'd probably try the .tag property of each button as a start.

As an aside, what is the method by which you intend to get this onto a mobile phone?
I don't know of any that run VB6 programs, but I'd be first in the queue if one existed..
 
Not suggesting stupidity for a moment.
Appforge looks very interesting and I wish you well in your endeavour.
Now I am second in the queue! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top