Valhalla Legends Forums Archive | General Programming | VB(HELP) -GetAsyncKeyState

AuthorMessageTime
MrRaza
I have an instant messenger program that sends data from the client to the server but i manually have to click a textbox named, Send. I want to send data by just hitting the 'Enter' key. I've got some idea where to start namely,

GetAsyncKeyState
GetActiveWindow


Some more help would be greatly appreciated.
March 16, 2003, 8:24 PM
haZe
Show me the code that runs when you click the send thing. I'll fix it to hit the enter key..
+hint: keyascii = 13 for enter+
March 16, 2003, 8:48 PM
MrRaza
I know keyascii = 13 for enter, im looking at code from pscode.com, im trying as hard as i can to figure it out myself so give me a day or two..
March 16, 2003, 8:54 PM
MrRaza
Alright, so far i added a timer to my form set the interval to 1 millisecond. I also added a module that has the following code:
[code]
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
[/code]
Im assuming that's all I need.

Now back to the main form.

You have to also declare GetAsyncKeyState on your form.
[code]Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer[/code]

I added the following code to the Private Sub Timer1_Timer() function.
[code]
Private Sub Timer1_Timer()
   
If Winsock1.State = sckConnected And KeyResult = GetAsyncKeyState(13) Then
       Winsock1.SendData txtSend.Text
       AddChat vbWhite, "<" & sUsername & ">" & _
                        vbGreen, txtSend.Text
End If
If Winsock1.State = sckClosed Then
   Timer1.Enabled = False
End If
End Sub
[/code]

This checks to see if the enter key has been hit, if it does get hit, then it sends the data.

Any ideas on how to improve this code are welcome. :)
March 16, 2003, 9:12 PM
Noodlez
that is so pointless, just go to the keypress event of your text box and check if keyascii = 13, i can't really tell what your talking about.. but if you mean another program, then subclass that textbox and look for char 13 then send the WM_CLICK message on the send button
March 17, 2003, 1:48 AM
haZe
[color=Red]
[code]
Private Sub txtSend_KeyPress(KeyAscii as Integer)
   
If Winsock1.State = sckConnected And KeyAscii = 13 Then        
Winsock1.SendData txtSend.Text
       AddChat vbWhite, "<" & sUsername & ">" & _
                        vbGreen, txtSend.Text
End If
If Winsock1.State = sckClosed Then
   Timer1.Enabled = False
txtSend.Text = ""
End If
End Sub
[/code]
[/color]
Your code with keyascii.
[move]$$$$$$$$$$$$$$$$[/move]
March 17, 2003, 7:14 AM
Grok
If this is your program, set the 'Default' property of the Send button to True.  When user hits enter, Send will be clicked.
March 17, 2003, 8:42 AM
Noodlez
haze, you should first check if keyascii = 13 then check if the socket is connected with another if
March 17, 2003, 8:31 PM
kamakazie
You should also use symbolic constants, as many have said before.  vbKeyReturn.
March 19, 2003, 1:04 AM
MrRaza@school
[quote]If this is your program, set the 'Default' property of the Send button to True.  When user hits enter, Send will be clicked.[/quote]

Yes, it is my program along with a classmates' at school. We are working on an instant messenger for a school project. Thanks for all your help btw.
March 19, 2003, 9:45 AM

Search