March 28, 2024, 06:22:39 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Can I "paste" characters into another program on my desktop?

Started by AdrianFox, March 12, 2010, 12:27:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AdrianFox

Is this possible in principle with a Windows function or command?

My Ebasic program selects a foreign language character which is copied into memory.  Currently, I simply paste it into Word or any other word processor with CTRL-V.
What I would ideally like to do is for the Ebasic program running in the foreground  to paste the character directly into the text in the word processing program in the background.

Is there a way of doing this?

Hope this isn't a dumb question.  Can it be done, and how do I go about achieving it?
Adrian Fox

LarryMc

March 12, 2010, 02:04:42 PM #1 Last Edit: March 12, 2010, 02:07:21 PM by Larry McCaughn
What little I know about that would make me say you don't really want to go there.

How would EBasic know where you wanted it pasted?

It would involve a lot of API's .

You're talking about code at Sapero's level if it can be done.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

AdrianFox

 ;)  I was hoping there might be just a single API call or something!  You're right, I don't want to go there.   Will have to be satisfied with copy and paste, which at least is easy to do now my little prog sits @TOPMOST.

Merci beaucoup for the advice.
:)
Adrian Fox

Copex

Quote from: AdrianFox on March 12, 2010, 12:27:20 PM
Is this possible in principle with a Windows function or command?

My Ebasic program selects a foreign language character which is copied into memory.  Currently, I simply paste it into Word or any other word processor with CTRL-V.
What I would ideally like to do is for the Ebasic program running in the foreground  to paste the character directly into the text in the word processing program in the background.

Is there a way of doing this?

Hope this isn't a dumb question.  Can it be done, and how do I go about achieving it?

Possibly, the code will load notepad and send a string of text, it emulates key presses. 


'load notepad and send text to window. Copex.

$include "windows.inc"
Declare Import,FindWindowA(lpClassName:Pointer,lpWindowName:string),Int 'windows.inc lpClassName is defined as string.

INT hParent

openconsole
'
SYSTEM "notepad.exe"  
print "asking notepad to load?"
_sleep(1000)


hParent = FindWindowA(NULL, "Untitled - Notepad")

if hParent <>0     'Valid window handle?
   _SetForegroundWindow(hParent)    'Show the window
   _SetFocus(hParent)               'Set window focus to new window

keyToPress( "Hello world\nHow are we to day" )

ENDIF

Print "press any key to end"

WAITCON

closeconsole

sub keyToPress(string txtToSend),int

string key=""
int loop=0

if len(txtToSend) = 0 then return FALSE

DO

loop++
key = mid$(txtToSend,loop,1)

     ' Simulate a key press
        _keybd_event( _VkKeyScan(key),NULL,NULL,NULL)
_sleep(50) '  Pause so it looks like someone typing can be removed not required

     ' Simulate a key release
        _keybd_event( _VkKeyScan(key),NULL,KEYEVENTF_KEYUP,NULL)
_sleep(50) ' Pause so it looks like someone typing can be removed not required

until loop = len(txtToSend)

return TRUE
ENDSUB
-
I really should learn how to use a spell checker! though im not sure how it will help someone who can not spell?
-
Except where otherwise noted, content Posted By Copex is
licensed under a Creative Commons Attribution 3.0 License

http://creativecommons.org/licenses/by/3.0/

AdrianFox

Hey, that's really cool!   

I can't pretend to understand how that works, but it gives me a good starting point to play with the code and see if I can adapt it to write my single character strings.

Thanks for the code.  I usually get there in the end by following the examples.
:)
Adrian Fox

ckoehn

Here is another one based on Copex's example.

$INCLUDE "windows.inc"

CONST Textbox=2

WINDOW mywin

OPENWINDOW mywin,0,0,200,160,@TOPMOST,0,"Special",&main
CONTROL mywin,@EDIT,"",40,5,150,100,@TABSTOP|@CTEDITLEFT|@CTEDITMULTI,Textbox

STARTTIMER mywin,250

run=1
WAITUNTIL run=0
CLOSEWINDOW mywin
END

SUB main
INT hwnd,awnd

SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW mywin
CASE @IDCLOSEWINDOW
run=0
CASE @IDTIMER
                        'I had to modify _FindWindow in windows.inc to
                         'DECLARE IMPORT, _FindWindow ALIAS FindWindowA(lpClassName AS POINTER, lpWindowName AS STRING),INT
hwnd=_FindWindow(NULL,"Untitled - Notepad") 'this makes sure your program is running
IF hwnd<>0
awnd=_GetForegroundWindow()
IF (awnd=hwnd) 'see if the foreground window is yours
_sleep(500) ' allow time for the other window to become active
KeyToPress(GETCONTROLTEXT(mywin,Textbox))
SETCONTROLTEXT mywin,Textbox,""
ENDIF
ENDIF
ENDSELECT
ENDSUB


SUB KeyToPress(STRING txtToSend),INT
STRING key=""
INT loop=0

IF LEN(txtToSend) = 0 THEN RETURN FALSE

DO
loop++
key = mid$(txtToSend,loop,1)

  ' Simulate a key press
_keybd_event( _VkKeyScan(key),NULL,NULL,NULL)
'_sleep(50) '  Pause so it looks like someone typing can be removed not required

  ' Simulate a key release
_keybd_event( _VkKeyScan(key),NULL,KEYEVENTF_KEYUP,NULL)
'_sleep(50) ' Pause so it looks like someone typing can be removed not required

UNTIL loop = LEN(txtToSend)

RETURN TRUE
ENDSUB

Copex

Quote from: AdrianFox on March 13, 2010, 01:03:14 PM
Hey, that's really cool!   

I can't pretend to understand how that works, but it gives me a good starting point to play with the code and see if I can adapt it to write my single character strings.

Thanks for the code.  I usually get there in the end by following the examples.
:)


Maybe this will help

http://msdn.microsoft.com/en-us/library/ms646304%28VS.85%29.aspx

-
I really should learn how to use a spell checker! though im not sure how it will help someone who can not spell?
-
Except where otherwise noted, content Posted By Copex is
licensed under a Creative Commons Attribution 3.0 License

http://creativecommons.org/licenses/by/3.0/

AdrianFox

Thanks both.  Gives me lots of scope to experiment and research.   Looks promising.
Adrian Fox

ckoehn

This example will paste whatever you type into the editbox into the next window that you click on.  The keyToPress is modified so that "SHIFT" is also send where needed.

$INCLUDE "windows.inc"

CONST Textbox=2

WINDOW mywin

OPENWINDOW mywin,0,0,200,160,@TOPMOST,0,"Special",&main
CONTROL mywin,@EDIT,"",40,5,150,100,@TABSTOP|@CTEDITLEFT|@CTEDITMULTI,Textbox

STARTTIMER mywin,250

run=1
WAITUNTIL run=0
CLOSEWINDOW mywin
END

SUB main
INT hwnd,awnd

SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW mywin
CASE @IDCLOSEWINDOW
run=0
CASE @IDTIMER
'hwnd=_FindWindow(NULL,"Untitled - Notepad") 'this makes sure your program is running
'IF hwnd<>0
awnd=_GetForegroundWindow()
IF (awnd<>mywin.hwnd) 'see if the foreground window is yours
_sleep(500) ' allow time for the other window to become active
KeyToPress(GETCONTROLTEXT(mywin,Textbox))
SETCONTROLTEXT mywin,Textbox,""
ENDIF
'ENDIF
ENDSELECT
ENDSUB


SUB KeyToPress(STRING txtToSend),INT
STRING key=""
INT loop=0,uc=0

IF LEN(txtToSend) = 0 THEN RETURN FALSE

DO
loop++
key = mid$(txtToSend,loop,1)
IF INSTR("ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|}{:?><~\"",key)>0  'shift needs to be down
_keybd_event( 0x10,NULL,NULL,NULL)
ENDIF
  ' Simulate a key press
_keybd_event( _VkKeyScan(key),NULL,NULL,NULL)
'_sleep(50) '  Pause so it looks like someone typing can be removed not required

  ' Simulate a key release
_keybd_event( _VkKeyScan(key),NULL,KEYEVENTF_KEYUP,NULL)
'_sleep(50) ' Pause so it looks like someone typing can be removed not required

IF INSTR("ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|}{:?><~\"",key)>0  'shift needs to be down
_keybd_event( 0x10,NULL,KEYEVENTF_KEYUP,NULL)
ENDIF
UNTIL loop = LEN(txtToSend)

RETURN TRUE
ENDSUB