?
Solved

How to cancel KeyDown Event for the 'F' key?

Posted on 2003-02-24
16
Medium Priority
?
1,537 Views
Last Modified: 2012-05-04
I need to capture Esc+F. I am able to capture it but I still get a F printed in textbox. How can i stop F from being printed whn escape is down?

I am using the code:

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = 27) Then EscapeDown = True
Select Case KeyCode
    Case vbKeyF
        If EscapeDown Then
            Text1.SelStart = i
            KeyCode = 0
            EscapeDown = False
        End If
end select
end sub

Sorry if this is simple but I am new to VB.
0
Comment
Question by:sambha03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +3
16 Comments
 
LVL 4

Expert Comment

by:bistrica
ID: 8014656
Esc is to be used alone.
With alphanumeric keys you can use: Shifr, Ctrl and Caps.
0
 

Author Comment

by:sambha03
ID: 8014680
Sorry but its a requirement of the project to capture Esc+F and Esc+R
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 8014751
Dim PrevChar as integer
PrevChar = 0

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
   Case vbKeyF
       If PrevChar = 27 Then
           Text1.SelStart = i
           KeyCode = 0
           EscapeDown = False
       End If
   end select
   prevchar = keycode
end sub

You just have to do it in 2 steps. The first time the user presses the escape, store it in a variable. Next time, he will press the f-key. Now you can see that the user has pressed the escape-char the last time.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:sambha03
ID: 8014770
I need esc to be pressed down when F is pressed. I am able to capture it but keycode=0 does not seem to wrk then.
0
 
LVL 4

Expert Comment

by:bistrica
ID: 8014845
If that is a requirement from teacher, than fine, you will learn something at teh end.

If that is requirement from application user, than you have to explain them that Esc+key is not standard requirement for GUI.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 8014849
What must happen then there ... you don't wont to display the f or what ?
Easy solution
text1.text = left(len(text1.text)-1)

Another solution:
Why don't you use the KeyPress-event ?

Dim PrevChar as integer
PrevChar = 0

Private Sub Text1_KeyPress(KeyAscii As Integer)
  Case vbKeyF
      If PrevChar = 27 Then
          Text1.SelStart = i
          KeyAscii = 0
          EscapeDown = False
      End If
  end select
  prevchar = keycode
end sub
0
 

Author Comment

by:sambha03
ID: 8018936
Thats an interesting idea Dhaest. I need to capture Ctrl+F and Esc +F. The Ctr+F wrks fine but Esc+F leaves a "F" on the textbox. So I can set a flag in KeyDown when Esc is pressed and reset it in KeyUP when Esc is released.I can then capture only Ctrl+F in KeyDown and capture F in keypress and check the flag to see if escape was pressed.Is this correct? Which one occurs first KeyDown or KeyPress , I thought KeyDown but before code enters "Case vbKeyF" in keypress, there is already a F on the textbox.
0
 

Author Comment

by:sambha03
ID: 8018944
text1.text = left(len(text1.text)-1) doesnt wrk since the cursor can be the middle of the single line text box ( by pressing left arrow after typing some text) when Esc+F was pressed.
0
 
LVL 1

Expert Comment

by:Vinay
ID: 8018979
You should use keypress insted of all other as its last event to get called in key events. So setting key code = 0 in other events wont have expected effect. The logic is same but just modified to suit ur requirements.

Better if u put this code in text box ocx and fire events when escape+F or escape+R is pressed. So you can have the code written in the event procedure itself!

Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Static prevkey As Integer
   
    ' To verify F or f or R or r pressed after escape
    If prevkey = 27 Then
        If (KeyAscii = 102 Or KeyAscii = 70) Then ' Code for F
            prevkey = KeyAscii
            KeyAscii = 0
        ElseIf (KeyAscii = 114 Or KeyAscii = 82) Then ' Code for R
            prevkey = KeyAscii
            KeyAscii = 0
        End If
    Else
        ' To store key status of previous keys
        prevkey = KeyAscii
    End If
End Sub

keep in touch
Vini!
vinusa500@yahoo.com
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 8018987
Just give it a try with the keypress-event
0
 

Author Comment

by:sambha03
ID: 8019019
Thanks Vinay but checking previous key would not wrk since I need the ESC key to be currently pressed down when F is pressed.
0
 
LVL 3

Accepted Solution

by:
Da_Weasel earned 200 total points
ID: 8021827
you will have to make use of all three key events and a global variable to signal when escape is pressed
in the key down event set the global var to TRUE
in the key up event set the global var to FALSE
in the keypress event if the global var is true and the key pressed was F or R then set KeyCode = 0, execute your Esc+char code for this event, and set the global value to false

Start a new standard EXE project and add a text box called Text1 and paste this code to see how it works:

Dim EscapeIsPressed As Boolean

Private Sub Form_Load()
EscapeIsPressed = False
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 Then _
        EscapeIsPressed = True
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If EscapeIsPressed Then
    If (KeyAscii = 102 Or KeyAscii = 70) Then ' Code for F
        KeyAscii = 0
        EscapeIsPressed = False
        MsgBox "Esc+F"
       
    ElseIf (KeyAscii = 114 Or KeyAscii = 82) Then ' Code for R
        KeyAscii = 0
        EscapeIsPressed = False
        MsgBox "Esc+R"
    End If
End If
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 Then _
        EscapeIsPressed = False
End Sub

0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8022940
Mr.Da Weasel's code working finely.
Hope That will Solove Sambha03 's Probs
0
 
LVL 1

Expert Comment

by:Vinay
ID: 8023270
U should run my code and see. I have tested it and working properly. I think no need to use 3 of functions as KeyPress is last event to get executed. So if one handles it correctly then it works fine.

Vini!
0
 
LVL 3

Expert Comment

by:Da_Weasel
ID: 8023425
Vinay: Your code does not accomplish the needed result.  He needs to detect when the Esc key and F or R are being pressed at the same time, not one after the other.
This means that the only way the special fucntionality should fire is when the Esc key is in the Down Position and then the F or R key it completely pressed.  Should the Esc key be released before the F or R is pressed then it should not fire an event.  For a solid bug free implementation of sambha03's needs all three events need to be used.

sambha03: As for your question about the order in which the events fire for keys.  They work as follows.
KeyPress signifies that the key was pressed and then released within a specified amount of time, usually about 500ms.  If you press the key and do not release it within that time frame then a KeyDown Event fires and when the key is released then a KeyUp Event fires.
So for any single press and release of a key you will either get a KeyPress event or KeyDown and KeyUp events.  It is possible for KeyDown/KeyUp events to fire by themselves with out the corresponding KeyDown/KeyUp event if say your control of form looses/gains focus in the middle of a keystroke, but generally you will get a KeyUp event for every KeyDown event.
0
 

Author Comment

by:sambha03
ID: 8023758
Thanks Da_Weasel , if i cud then i'll give you something higher than A.It wrks beautifully :-) Thanks to Vinay adn all others for helping out.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 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