Last Key Pressed/Move In Direction

K

Kevin Sprinkel

I am simulating an Excel spreadsheet with overlapping
controls, txtFormula and txtResult. The result is
normally displayed. When the user enters the cell, the
OnFocus event procedure disables the cell and makes it
invisible, and sets the focus to the txtFormula control.
The txtFormula_Exit procedure then resets the control
properties.

Because of the Tab Order, passing through the controls is
different in one direction from the other. I thought a
solution might be to determine what key was pressed on
entering the txtResult control, and let the txtFormula
LostFocus procedure move the focus explicitly based on the
key's value.

Can anyone tell me how I might do this?

Thank you.

Private Sub txtResult_GotFocus()
On Error Resume Next
With Me!txtFormula
.Enabled = True
.Visible = True
.SetFocus
End With
With Me!txtResult
.Enabled = False
.Visible = False
End With
End Sub


Private Sub txtFormula_LostFocus()
On Error Resume Next
With Me!txtResult
.Enabled = True
.Visible = True
End With
With Me!txtFormula
.Enabled = False
.Visible = False
End With
End Sub
 
R

Ragnar Midtskogen

You can use the Windows API function GetKeyState.

Here is an example where I check if the key "d" has been pressed.

Public Function KeyDown(KeyCode As Long) As Boolean

Dim nRetVal As Integer
Dim nMask As Integer

nMask = &H8000

nRetVal = GetKeyState(lnVirtKey:=KeyCode)
If ((nRetVal% And nMask%) = nMask%) Then
KeyDown = True
Else
KeyDown = False
End If

End Function

You can find all the key codes, as well as more details on GetKeystate, by
searching with Google for example

Ragnar
 
A

Allen Browne

You already have one answer, but
Screen.PreviousControl
may be helpful in figuring out which direction the user is going.
 
K

Kevin Sprinkel

Thanks as always, Allen.
-----Original Message-----
You already have one answer, but
Screen.PreviousControl
may be helpful in figuring out which direction the user is going.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.




.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top