Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Nov 20, 2019 10:28 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Sending Key Strokes (SendInput ?)
PostPosted: Fri Mar 02, 2012 6:03 pm 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
Hi

I've had made an English-Persian Dictionary and i wanted to port it to RS

Well, my question is how to Register a Global Key combination and then how to send a keystroke (Ctrl+C).

as you may guess i wanna get the Selected Text (Highlighted by User) for translation
I figured out to send a Ctrl+C and then get the text from Clipboard.

Is there any More Standard way to achieve this ?

So, for Sending Key-Stroke, I tried to use SendInput WinAPI Function (From a VB Code) but no success

Const VK_H = 72
Const VK_E = 69
Const VK_L = 76
Const VK_O = 79
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
uMsg As Long
wParamL As Integer
wParamH As Integer
End Type
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_KeyPress(KeyAscii As Integer)
'Print the key on the form
Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'Clear the form
Me.Cls
'call the SendKey-function
SendKey VK_H
SendKey VK_E
SendKey VK_L
SendKey VK_L
SendKey VK_O
End Sub
Private Sub SendKey(bKey As Byte)
Dim GInput(0 To 1) As GENERALINPUT
Dim KInput As KEYBDINPUT
KInput.wVk = bKey 'the key we're going to press
KInput.dwFlags = 0 'press the key
'copy the structure into the input array's buffer.
GInput(0).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'do the same as above, but for releasing the key
KInput.wVk = bKey ' the key we're going to realease
KInput.dwFlags = KEYEVENTF_KEYUP ' release the key
GInput(1).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'send the input now
Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub


Thanks In Advance


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Fri Mar 02, 2012 6:53 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Take a look at the VB Compatibility module of the Windows Functionality Suite (included with RealStudio in the Example Projects directory.) It includes the SendKeys subroutine, which should provide the functionality you want.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Fri Mar 02, 2012 7:04 pm 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
Thanks for your helpful reply :)

But how can i register a global Key-Combination, so App can capture it whether Window is focused or not (e.g Call this "SendKey" Function when Ctrl+Q is Pressed)


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Fri Mar 02, 2012 7:16 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Also in the WFS, look for the "Hotkey Helper" module/class.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Sat Mar 03, 2012 11:40 am 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
I don't Know how to use this class (A beginners Question).

How can i use this class in my project and Register the HotKey ?


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Sat Mar 03, 2012 11:57 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Drop it onto a window then register your hotkeys with the RegisterHotKey method. This method takes two parameters: a key modifier and a key scan code. Modifiers and a virtual key code (see the RegisterHotKey documentation for these values. The RegisterHotKey method returns an integer which will be used to identify future hotkey presses to you, so save it somewhere accessible: it's the 'identifier' argument supplied to the HotKeyPressed event of the HotKeyHelper class, which is the code that gets executed for every hotkey that gets pressed (so put your logic there.)

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Mon Mar 05, 2012 10:02 am 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
Thanks, You helped me a lot :)

I Registered the Hotkey (Ctrl+Q), And the identifier is saved in HKey Property
This is the code i wrote in HotkeyPressed Event of HotkeyHelper class:
If identifier = Window1.HKey Then
Dim clp As New Clipboard
Dim OldClip As String
OldClip = clp.Text
VB.SendKeys("^c")
If clp.TextAvailable Then

MsgBox(clp.text)
Clp.SetText(OldClip)
Else
MsgBox("NotAvailable")
End
clp.Close
End If

I dont know whats wrong, this code should send a Ctrl+C and copy the highlighted text to the clipboard, then it should be accessible by "clipboard.Text" method, but it doesnt work.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Mon Mar 05, 2012 7:08 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
The app you're trying to copy text from has keyboard focus, right?

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Mon Mar 05, 2012 7:21 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You're reading the clipboard BEFORE you send the keys. So as of the time you say "new Clipboard", the clipboard does not contain the text. Try sending the keys first, then instantiate the clipboard.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Mon Mar 05, 2012 9:43 pm 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
I found the problem, it seems that there is a delay in copying the selected text to the clipboard (using Control+c), so when code tries to read the clipboard text, it has not been copied to it yet !
i tried this code and it works:
Dim Clip As New Clipboard
VB.SendKeys("^c")

For i As Integer = 1 To 5000
If i=5000 Then
Dim Text As String = Clip.Text
MsgBox(Text)
Clip.Close
End
Next

So, is there any command to pause the code execution for certain amount of time or i should stick to this method ?, it seems a little non-Standard !


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Mon Mar 05, 2012 10:45 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Try app.SleepCurrentThread.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Wed Mar 28, 2012 5:49 pm 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
timhare wrote:
You're reading the clipboard BEFORE you send the keys. So as of the time you say "new Clipboard", the clipboard does not contain the text. Try sending the keys first, then instantiate the clipboard.

My problem is solved, But to learn better how thing work.

Would you explain it to me, Why its better to instantiate the Clipboard after it contains something ?
before you said that, i thought, i instantiate the class, so somewhere in the code i could use its methods, and so i thought actually when .Text Method is called, the class tries to Get clipboard contents (As Tex).

So, how does really things work ?
What happens when a class is instantiated ?

Thanks


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Wed Mar 28, 2012 6:14 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
The clipboard class is a snapshot of the clipboard's contents at the time it is instantiated. It is best to instatiate a new clipboard object just before you want to read it.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sending Key Strokes (SendInput ?)
PostPosted: Wed Mar 28, 2012 6:44 pm 
Offline

Joined: Sun Jul 31, 2011 11:30 pm
Posts: 31
Location: Iran
Thanks


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group