|
-
Jun 6th, 2000, 01:18 AM
#1
Thread Starter
Hyperactive Member
If keypress key_y then
msgbox "Y"
end if
when the y key is pressed i should get a msgbox that says y, y can't i?
-RaY
VB .Net 2010 (Ultimate)
-
Jun 6th, 2000, 02:02 AM
#2
Addicted Member
This is from vb6 and works if shift-y or y is pressed. Notice that the ascii code for y is 121, Y is 89
(KeyPreview property of the form is True)
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 89 ' Y
MsgBox "Y pressed"
Case 121 ' y
End Select
End Sub
[Edited by LAURENS on 06-06-2000 at 03:10 PM]
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Jun 6th, 2000, 02:02 AM
#3
Keypress is an Event associated with a number of objects including TextBoxes. Place the following code in the Keypress event of the object you are interested in.
Code:
Select Case KeyAscii
Case 89
MsgBox "Y"
Case 121
MsgBox "y"
Case Else
MsgBox "The " & Chr(KeyAscii) & " key was just pressed and it has a KeyAscii value of " & KeyAscii
End Select
-
Jun 6th, 2000, 03:40 AM
#4
Thread Starter
Hyperactive Member
Thanks again
Thanks again, im gonna give it a wheirl...
-RaY
VB .Net 2010 (Ultimate)
-
Jun 6th, 2000, 03:42 AM
#5
transcendental analytic
' For both upper and lowercase:
If lcase(chr(keyascii))="y" then msgbox "y"
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 6th, 2000, 04:10 AM
#6
Thread Starter
Hyperactive Member
Almost got it
Ok this works, however it "Must be in the keypress event" I got my code in timer1, with interval set to 1 and value true....
Sub Timer1_Timer()
If Key_press & lcase(chr(keyascii)) = "y" then msgbox "TIMER GOT YA"
end if
but it doesnt really catch it ;o\
-RaY
VB .Net 2010 (Ultimate)
-
Jun 6th, 2000, 04:22 AM
#7
I hope this doesn't offend you, but I really think you need to step back and study some of the basics of VB coding before you do anything more. There a lot of good books available and while they are now usually aimed at VB5 or VB6, much of what they say will still apply to VB3. You might check the library however, they might still have books specific to VB3. The reason I say this is that your code that you just posted doesn't make a lot of sense.
The first thing you should do is in the Declarations section of each of your forms and code modules, add Option Explicit. This will allow VB to point out to you when it doesn't understand a variable. It will also force you to Dim all your variables, but that's something you should do anyhow.
Getting back to the code you just posted, which was
If Key_press & lcase(chr(keyascii)) = "y" then msgbox "TIMER GOT YA"
end if
here is what it's saying to VB:
"If some variable which I haven't defined, called Key_press is concatenated with the letter "y" then issue a message."
-
Jun 6th, 2000, 05:52 AM
#8
New Member
MartinLiss is right, I don't understand the Key_press thing. There's no such constant or variable in VB5 or VB6, not to mention earlier versions of VB. The KeyAscii variable will work only if you put your code under the KeyPress Event or any other events that has this: '(KeyAscii as Integer)'.
Originally posted by MartinLiss
I hope this doesn't offend you, but I really think you need to step back and study some of the basics of VB coding before you do anything more. There a lot of good books available and while they are now usually aimed at VB5 or VB6, much of what they say will still apply to VB3. You might check the library however, they might still have books specific to VB3. The reason I say this is that your code that you just posted doesn't make a lot of sense.
The first thing you should do is in the Declarations section of each of your forms and code modules, add Option Explicit. This will allow VB to point out to you when it doesn't understand a variable. It will also force you to Dim all your variables, but that's something you should do anyhow.
Getting back to the code you just posted, which was
If Key_press & lcase(chr(keyascii)) = "y" then msgbox "TIMER GOT YA"
end if
here is what it's saying to VB:
"If some variable which I haven't defined, called Key_press is concatenated with the letter "y" then issue a message."
PiKaPrO
=======
PiKa ProGraMMeR © 2000
[email protected] No SPAMMERS!
MSVB6.0 PRO SP3 Detected.
-
Jun 6th, 2000, 06:30 AM
#9
transcendental analytic
And learn to try to avoid timers and have event executing your code instead.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 6th, 2000, 01:29 PM
#10
Addicted Member
Kedaman,
What's wrong with using a timer (1 per app max.)? (In general, not for the question in this thread)
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Jun 6th, 2000, 07:18 PM
#11
Thread Starter
Hyperactive Member
Itz all good
Welp, the reason why really didn't touch up on the book yet is because eventually i will take classes in it. Right now im getting all my pre reqs out of the way. Which is all i really have time to study for now. The reason why is used the Key_Press() event, is because i saw a code that called that function everytime the timers interval hit "1"...so basically it called the key_press event, when keys aren't pushed.....and in the key_press event i have the constant of the keys, hopefully being able to msg me when any of those keys(constants) where pressed. And nope I don't get offended, i was just trying to conjure and alternative to the other code that does work since when i try to recorde keystrokes vb3, doesnt support "user32" and gets all messed up doesn't work....anyways thanks for the input!!!
-RaY
VB .Net 2010 (Ultimate)
-
Jun 6th, 2000, 07:47 PM
#12
transcendental analytic
Laurens, Nothing is wrong, but it is highly unprofessional, timer is both inaccurate and allows threading following an order you can't predict, when you have threading with doevents. Timer is also a control, you can do the same with api (settimer and killtimer) but I recommend to use Loops with doevents threading.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 6th, 2000, 11:06 PM
#13
Unprofessional?
I don't know kedaman's background and/or training, but as a professional let me say that it's not "highly unprofessional" to use timers. I wouldn't use one to check to see if a user pressed a key, but that doesn't mean that there aren't perfectly legitimate times when you can use one.
-
Jun 7th, 2000, 12:18 AM
#14
transcendental analytic
Well, ok, Martin you can use the timer for a lot of stuff, but you can also do that the other more professional way. If the timer control was in a independent library i would leave it.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 7th, 2000, 01:43 PM
#15
Addicted Member
Kedaman,
I use a timers to check the existance of new files every few minutes or so.
The way you describe sounds like very resource consuming to me. Sure, Timer is a control, but I feel that is an advantage because it acts more or less independant to a certain degree. If you program DoEvent loops your program will be (very) busy even if the user is out for lunch, or is using some other app with your program in the background.
By the way, I wouldn't call the way other people work higly unprofessional. Most people decides on something because it fits their purposes. Most of us got to have their app's up and running as quickly as possible. I'm not so sure if reinventing the wheel would be much of a help then.
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Jun 7th, 2000, 03:16 PM
#16
transcendental analytic
Sorry, Laurens, don't get too upset about what i'm saying, use your timer for whatever you want, it works and doesn't crash and that's a good feature for a timer. But one thing i have to correct you, doevents loops wont keep a program more busy than it needs, at least if you place doevents in the right places. I'm taking back that word unprofessional, if that sounds too agressive.
Thanks.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 7th, 2000, 03:30 PM
#17
Addicted Member
Dont't worry, I'm not upset, just curious.
Could you show me some code of a loop with DoEvents that doesn't keep your computer (not just your program) more busy ?
I'm assuming you use such a loop as an alternative to a timer, that runs througout the whole session. I did a very little test with it, CPU activity went up to 100%, but since I still could do other things because of the doevents that doesn't bother me too much. It sure slowed things down though.
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Jun 7th, 2000, 03:55 PM
#18
transcendental analytic
so, CPU went up to 100%, that's only one explanation, while there's no other processes, your code will be executed. That doesn't mean your other applications can do their work in normal performance. But for showing a good example of use with doevents, that's pretty useless because all loops have different tasks to performe. The more intensive, the more doevents you need. And you need to put them in strategic places where they can't be too overused. It will drain on performance.
Do while active
'Code...
Doevents
Loop
Another feature is when you close your application while a loop is running, it may hang up. To solve this your loop must have a exit path, the boolean (active) condition (you can have that one public in a module if you have several forms.
For code executed while your app runs independent from which form is closed or not, you could have sub main in a module as the startup object and run the code loop there. If you want to split parts of code to be execute in different intervals, use the same loop, put gettickcount api to count the intervals and use separate variables to wait for an active condition.
Code:
secloop = GetTickCount: msloop = secloop
Do While Forms.Count
If msloop < GetTickCount Then
msloop = msloop + 1
'code executed each millisecond
End If
If secloop < GetTickCount Then
secloop = secloop + 1000
'code executed each second
End If
For temp = 0 To threadconst 'Threading
DoEvents
Next temp
Loop
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 7th, 2000, 05:17 PM
#19
Addicted Member
Ok,
so the slowing down will be minimal (not absent though, the loop will be pretty tight).
But I still don't see the advantage over a timer. As far as I can see it, this is exactly what the timer does except that it doesn't execute code but raises an event.
I have app's running serverside. As you understand the servers are up permanently (Yes, this is sometimes possible with NT) and so are these app's. I never had a problem with a timer there.
The solution you present seems to have a problem after about 24 days. Won't GetTickCount return a value less than your stored value sooner or later ? It returns a long which will be reset sooner or later. You will need some overflow testing like
' ---- CODE NOT TESTED ----
If (GetTickCount Mod cMaxLongValue) < lngTestOverflow Then
' Reset variables.
lngSecLoop = GetTickCount
lngMSLoop = lngSecLoop
'
lngTestOverflow = GetTickCount Mod cMaxLongValue
'
End If
'
lngTestOverflow = (GetTickCount Mod cMaxLong)
'----- END CODE NOT TESTED ---
PS. How do you get your code formatted properly in this forum?
[Edited by LAURENS on 06-08-2000 at 12:20 PM]
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Jun 7th, 2000, 07:55 PM
#20
Frenzied Member
Why not forget all about timers, switch the form's KeyPreview property to TRUE and put the code in the form's KeyPress event - then as long as the form is open any keypresses will be caught by that event first so you can do whatever it is you want to do...
I agree that using a Timer in this example is bad programming practice - but there are times (excuse the pun) when Timers are the only answer.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 7th, 2000, 08:09 PM
#21
Addicted Member
I complete lost sight on what this thread was about. You're right Buzby, that talk about timers isn't really contributing to the actual topic.
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Jun 7th, 2000, 08:17 PM
#22
transcendental analytic
Disadvantages with timer
- It's a Control
- It's inaccurate to 60ms
- It will can't fire event's in a smaller interval than 53ms
- You must use several timers to fire several events which intervals is not proportional to each other
- Will inhibit event in other loops with doevents
- Must be in a form and fire a form event. So it's not independent
Advantages with timer
- No unnessesary CPU usage, the system will use it between the intervals.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 7th, 2000, 08:20 PM
#23
Addicted Member
Ok, that's clear.
PS. You forgot 'Requires no programming' as an advantage.
PSS. Actually it fires an event about every 10ms at most.
GetTickCount in a 1 ms interval timer gives this:
26702466
26702476
26702486
26702496
26702506
26702516
26702526
26702536
26702546
26702556
26702566
26702576
26702586
26702596
26702606
26702616
26702626
26702636
26702646
26702656
26702666
26702676
26702686
26702696
26702706
26702716
26702726
26702736
26702746
26702756
Now I really quit with this timer stuff.
[Edited by LAURENS on 06-08-2000 at 03:44 PM]
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|