Results 1 to 20 of 20

Thread: Keyboard

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110

    Question Keyboard

    I need to make 3 keys (1,2,3) to pause the program while it is running, start it again, and exit when its done. I figured i'd have to use keypress but i dont know where to start.

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Is this what you wanted?
    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2. Select Case KeyAscii
    3.     Case Is = vbKey1
    4.     MsgBox "Pressed 1"
    5.         'code for KeyPressed = 1
    6.     Case Is = vbKey2
    7.     MsgBox "Pressed 2"
    8.         'code for KeyPressed = 2
    9.     Case Is = vbKey3
    10.     MsgBox "Pressed 3"
    11.         'code for KeyPressed = 3
    12. End Select
    13. End Sub

  3. #3
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    i dunno what your program does, but if there a lot of process,
    you might want to think of putting a least one DoEvents
    to let the user do a KeyPress!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110

    here it is

    Here's the deal with the program. This program runs tests on power supply units. The information from the test is stored in a text box. I need Key 1 to Pause the program, Key 2 to Restart it, and Key 3 to Exit. Im working with old QBasic code, about 13 yrs old, and i am converting it to VB and there's no notes anywhere on the code so im having some trouble. The person who wrote the QB code set the keys as:

    On Key(1) GoSub CSOF
    On Key(2) GoSub PAUSE
    On Key(3) GoSub MANUAL_INPUT
    On Key(4) GoSub ADJUST
    On Key(5) GoSub RESTART

    CSOF being a line to jump to, the same for pause, etc.
    CSOF is the exit code.

    I figured i could rewrite these lines into sub routines but i don't know how to get the Keys to work properly. I tried running a test for using the number key 1 as an exit button with little success.

    Private Sub Form1_Keypress(Ascii As Integer)
    If KeyAscii = vbKeyReturn Then
    Unload Me
    End If

    End Sub

    I don't know how i would go about pausing the program or restarting it. Any thing else you'd like to know?

  5. #5
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    api

    here is an API call. I've never used it but it is supposed to
    pause program execution.

    Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110

    api

    i need it to pause until i hit a key to restart the program.

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    How are you defining PAUSE.
    If it is program execution, you can handle it in the Form_KeyPress event or
    If you want to clear the screen use a loop in the event to set
    visible = false for all the controls on the form

  8. #8
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Private Sub Form1_Keypress(Ascii As Integer)
    If KeyAscii = vbKeyReturn Then
    Unload Me
    End If

    End Sub
    This does not work because, either
    (a) You do not have the KeyPreview property of the form set to true

    or

    (b) The first focus on load is a command button

    If the latter, I suggest you set the tabindex of label on your form to 0, so that the focus begins from there.

    Hope I am making some sense

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110
    makes a ton of sense, but nothing still. no command button, BUT you were right about the KeyPreview property being false. I set that to true and nothing still. But thats ok, im sure i will figure that out....im more bothered by the pausing of the program and starting it from where it left off again......

  10. #10
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    If your app does intensive work, as Sebs mentioned, have well spaced DoEvents, to capture the Keystroke.

    Once the keystroke is captured, the easiest (and the least preferrable) way is to simulate a single "CTRL+ALT+DEL" using Sendkeys or something else. This effectively suspends (pauses) the OS. Of course if you want to run other apps while fixing equipment, hooking up cables, etc are done, then of course......

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110

    Maybe this can help.....

    This is the old QBasic code with the pause being called:

    On Key(2) GoSub PAUSE 'At beginning of program

    PAUSE:
    ST11! = Timer
    Call KY
    ST! = ST11!
    Return

    On Key(5) GoSub RESTART

    RESTART:
    START_FL = 5
    Return
    RESTART1:
    Close
    TOTAL_STATUS$ = "Failed"
    Call SOF
    Color 14, 0
    CLS
    Call Init
    TI! = Timer
    GoTo 108

    Now i know about the timer but it doesn't really tell mehow it's used, also it would help if i knew more QBasic. but this is basically how its done in the old program and im having trouble replicating it.

  12. #12
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Tried using the timer, Gets stuck!!!

    A point: If the purpose of the pause is to give you time to do some physical changes to the Machine, Can you provide a long
    enough PAUSE interval, say 5 minutes, or something, if so then you could "pause"
    execution on receiving a keystroke like so
    VB Code:
    1. Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
    2.  
    3. Sub Pause(Duration As Double)
    4.     'Pause (300) 'pauses for 5 minutes
    5.  
    6.     Dim start As Double
    7.     start# = GetTickCount 'store milliseconds since system start
    8.  
    9.     Do: DoEvents
    10.         On Error Resume Next
    11.     Loop Until GetTickCount - start# >= (Duration# * 1000) 'loop until the actual time (minus stored time) is greater than or equal to the duration (seconds * 1000 = milliseconds)
    12. End Sub

    But if you finish your job in less than 5 mins, you still have to wait till this loop gets through.

    helping you?

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110
    I was thinking about that.....ill have to find out the average time a pause lasts and just use that for the time being. I have other things i need to deal with at the moment. Someone mentioned to me using an API function and a sleep function on top of that. Haven't looked into it yet. Thanks KayJay ill try that out for now but i know i will have to change it eventually. Any other ideas would be great.

  14. #14
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    This is a long thinking-off-head post. So please bear with me!. It seems to work on my machine.

    You have TWO apps running

    One is your application: APP 1

    The other has two buttons, "PAUSE", "RESTART": APP 2

    In APP 1 have timer set to, say, 1 second.

    Every second, in the timer event, check for a string value in a file "c:\pause.txt"

    If the value is "False", it exits the subroutine

    When you click the "PAUSE" button in APP 2, the "False" value is changed to "TRUE"

    When the timer event fires again it reads a "TRUE" value and runs an indefinte loop.

    When you press the "RESTART" button in APP 2, the value is reset to "FALSE".

    The Timer event now reads a "False" value and exits the sub routine

    Think this will work for you?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110
    That may work, ill have to try it out. Can you implement that same idea in one app? cause it makes a lot of sense. Say press pause, it jumps to the timer event somewhere in the App and gets looped, then the second you hit restart, it jumps back to where it left off. I think we're getting somewhere. haha. im gonna try this.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110
    Ok so i can't think right now my mind is going way to nuts, cant even think of how to code this at the moment. Ill give it a shot when i go home tonight. Thanks a ton kayjay. I shall post my results later.

  17. #17
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    This is working for me fine now. I dont know why, but 4 DoEvents seem to do the trick
    VB Code:
    1. Dim mloop As Boolean
    2.  
    3. Private Sub Form_KeyPress(KeyAscii As Integer)
    4. If KeyAscii = vbKey2 Then 'pause
    5.     mloop = True
    6. ElseIf KeyAscii = vbKey4 Then 'continue
    7.     mloop = False
    8. ElseIf KeyAscii = vbKeyReturn Then
    9.  End
    10. End If
    11. End Sub
    12.  
    13. Private Sub Timer1_Timer()
    14. Do While mloop = True
    15.     Debug.Print "looping"
    16.     DoEvents
    17.     DoEvents
    18.     DoEvents
    19.     DoEvents
    20.     If mloop = False Then
    21.         Exit Sub
    22.     End If
    23. Loop
    24. End Sub
    25.  
    26. Private Sub Form_Load()
    27. Label1.TabIndex = 0
    28. End Sub

    Check it out and let me know

  18. #18
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Take care

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    NJ
    Posts
    110
    Hey thanks. I just tried it out and it works for me so far. I still wanna try the timer idea. Also....tell me what you think of this?

    What if i have Key 2 create a message box to pop up with just a simple OK. Wouldn't this freeze the program until ok is clicked again? just a quick thought.

  20. #20
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Originally posted by runt1128
    What if i have Key 2 create a message box to pop up with just a simple OK. Wouldn't this freeze the program until ok is clicked again? just a quick thought.
    Message box freeze the program when it is run not compiled but once compiled it will only stop the module from where is displayed but not timers or other procedures already running.
    Josep Mª

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