Link to home
Start Free TrialLog in
Avatar of Canon
Canon

asked on

timer problem

Hello all;

I have a form with a textbox on it and an array of buttons with the alphabet on them. The idea is for the user to click the buttons with a mouse and the letters will be placed into the textbox.

I didn't want the user to use the keyboard (mouse only) so i placed a timer on the form to check the keys pressed and if pressed it would call a function warning them not to use the keyboard.

My problem is what code would I place into the timer below which would allow the user to click the mouse on the button array, have the letter entered into the textbox but not trigger the Mobile1 function should the keyboard be used instead of the mouse:

Private Sub Timer1_Timer()
Dim KeyPressed As String
       Dim Counter As Integer
       For Counter = 1 To 255
         If GetAsyncKeyState(Counter) <> 0 Then
           ' Check if key has been pressed
           KeyPressed = CheckKey(Counter)
            If KeyPressed <> "" Then
   'Key was pressed, keyboard being used, call Mobile1
             Call Mobile1
             End If
         End If
       Next
End Sub

Thanks in advance

Canon
Avatar of Crash2100
Crash2100
Flag of United States of America image

What are you trying to prevent by disabling the key?

This would stop them from being able to enter anything into the textbox:

Text1.Enabled=False

--OR--

Private Sub Text1_KeyPress(KeyAscii As Integer)
   KeyAscii = 0
End Sub
Avatar of Canon
Canon

ASKER

Hello Crash2100;

I don't want to stop them from entering anything into the textbox. I want to limit them to using the "mouse only" to do it. They can click the "button array" with the mouse and this enters the letters/numbers they choose into the textbox.

My problem is that I need for them to do it with the mouse only and not the keyboard. I can disable the keyboard using the "Mobile1" function in the timer example above but when they click the button array, it seems to defeat my purpose for the Mobile1 function.

I need to be able to block the keyboard (Mobile1 function takes care of this) but at the same time, allow the mouse "button array" clicks to send the data to the textbox without the Mobile1 function interferring with these buttons being clicked " whew  ".

Hope this makes more sense now. Don't worry about why it needs to be done, just need to know if it can be done and how with working code by modifying my example of the timer above:)

Thanks for commenting!

Canon
Avatar of Richie_Simonetti
Why don't you use a flag in Keypress event allowing to type when you want and disable it when you need?
Private Sub Text1_KeyPress(KeyAscii As Integer)
if flag then ' not allowed
   keyascii=0
End Sub
Avatar of Canon

ASKER

Hello Richie_Simonetti;

Thanks for your comment! I think the beauty of this question is how can one set a timer to call a function for monitoring the keyboard keystrokes and thus disabling it and at the same time allow text buttons to be clicked by the mouse and the data entered into a textbox without triggering the function :) ?

I am much better at asking a question than I am at programming so when you mention ""Why don't you use a flag in Keypress event allowing to type when you want and disable it when you need?
Private Sub Text1_KeyPress(KeyAscii As Integer)
if flag then ' not allowed
  keyascii=0
End Sub ""

I am not quite sure how to go about trying what you have suggested? Can your example be placed into the timer in such a way as to accomplish my goal ?

Thank You

Canon

Avatar of Canon

ASKER

Hello Richie_Simonetti;

Thanks for your comment! I think the beauty of this question is how can one set a timer to call a function for monitoring the keyboard keystrokes and thus disabling it and at the same time allow text buttons to be clicked by the mouse and the data entered into a textbox without triggering the function :) ?

I am much better at asking a question than I am at programming so when you mention ""Why don't you use a flag in Keypress event allowing to type when you want and disable it when you need?
Private Sub Text1_KeyPress(KeyAscii As Integer)
if flag then ' not allowed
  keyascii=0
End Sub ""

I am not quite sure how to go about trying what you have suggested? Can your example be placed into the timer in such a way as to accomplish my goal ?

Thank You

Canon

1) Get rid of the timer.  It should not be used for this.  

2) use the above code
Private Sub Text1_KeyPress(KeyAscii As Integer)
  KeyAscii = 0 ' cancels the key stroke in the text box
End Sub

3) If you want to tell the user to not use the keyboard, add a message box to the code in 2)

Private Sub Text1_KeyPress(KeyAscii As Integer)
  MsgBox "Do not use the keyboard!"
  KeyAscii = 0 ' cancels the key stroke in the text box
End Sub

Are you using something other than the text property of the textbox control to change what it displays?  If you use the text property, it will not fire the KeyPress event.
If you want to cut a tree you use the appropriate tool.
You are not going to use a fork to do it.
Now for your problem, don't use a timer for that.
Richie and Ischuele are perfectly right.

Rem

ASKER CERTIFIED SOLUTION
Avatar of Madmarlin
Madmarlin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry that should have been a comment
Avatar of Canon

ASKER

Hello Madmarlin;

Gonna give you the points and a good grade! The reason is because none of the above comments were what I needed and figured since you locked this question why not ? Har!

I will keep working on my problem and will eventually figure it out. Just wanted to know if there was a way to do it using the timer and my original scenario that was posted to speed things up a little for me:)

Hey Rem, That was funny about the fork and the tree! Reminds me of "is the house on fire or half on fire"! I left out a lot of code and the reason for the way I wanted to do things with the timer. Just because things are not conventional doesn't mean they aren't capable of being operational:)

For instance, why not use a timer and waste resources if it is to be used for 5 seconds and takes care of everything that you needed to do? Down and Dirty perhaps but a solution never the less!

Thanks to all!

Canon
I hope you'll work it out.
Mind though you are a bit unfair with regards to the people who gave you the code (crash2100 richie etc.)
Even madmarlin acknowledges that.
Ask a moderator to split the points. (just to be fair)

Rem