• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1677
  • Last Modified:

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

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
sambha03
Asked:
sambha03
  • 6
  • 3
  • 2
  • +3
1 Solution
 
bistricaCommented:
Esc is to be used alone.
With alphanumeric keys you can use: Shifr, Ctrl and Caps.
0
 
sambha03Author Commented:
Sorry but its a requirement of the project to capture Esc+F and Esc+R
0
 
DhaestCommented:
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.

 
sambha03Author Commented:
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
 
bistricaCommented:
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
 
DhaestCommented:
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
 
sambha03Author Commented:
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
 
sambha03Author Commented:
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
 
VinayCommented:
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
 
DhaestCommented:
Just give it a try with the keypress-event
0
 
sambha03Author Commented:
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
 
Da_WeaselCommented:
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
 
sazhagianambiCommented:
Mr.Da Weasel's code working finely.
Hope That will Solove Sambha03 's Probs
0
 
VinayCommented:
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
 
Da_WeaselCommented:
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
 
sambha03Author Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now