Solved

timer problem

Posted on 2002-04-24
12
673 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:Canon
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 18

Expert Comment

by:Crash2100
ID: 6967559
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
0
 

Author Comment

by:Canon
ID: 6967624
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6967648
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
0
 

Author Comment

by:Canon
ID: 6967692
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

0
 

Author Comment

by:Canon
ID: 6967693
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

0
 

Expert Comment

by:lschuele
ID: 6967768
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:Rem
ID: 6967817
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

0
 
LVL 1

Accepted Solution

by:
Madmarlin earned 200 total points
ID: 6967887
I agree see no reason why a timer would hodl any advantage the code the others have provided is perfect.

Also if you have a timer to kick off over a short period you are using resources that are not required.
Using the keypress event only will erradicate haveing to take up the resources used by the timer..

The below syntax is all you need

Private Sub Text1_KeyPress(KeyAscii As Integer)

'Tell user that the keyboard cant be used
 MsgBox "Kayboard Entery Has Been Prohibited"

'Setting KeyAscii to 0 means that the action is pretty much ignored.
 KeyAscii = 0

End Sub


Good Luck
0
 
LVL 1

Expert Comment

by:Madmarlin
ID: 6967889
Sorry that should have been a comment
0
 

Author Comment

by:Canon
ID: 6968666
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
0
 
LVL 3

Expert Comment

by:Rem
ID: 6968720
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6968724
:O
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now