Results 1 to 23 of 23

Thread: one more time

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Unhappy

    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)

  2. #2
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Thanks again

    Thanks again, im gonna give it a wheirl...
    -RaY
    VB .Net 2010 (Ultimate)

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ' 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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    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)

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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."

  8. #8
    New Member
    Join Date
    Jun 2000
    Location
    Singapore
    Posts
    8

    Angry

    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    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)

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  15. #15
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  17. #17
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  19. #19
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  20. #20
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    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."

  21. #21
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  23. #23
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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
  •  



Click Here to Expand Forum to Full Width