inkey coy paste

Posted By: Reconnoiter

inkey coy paste - 03/30/15 12:08

Hi,

Anyone know how to paste text (from the windows copy /ctrl+c) in a inkey string or other normal string?

Maybe through a windows function?

Or is this impossibru?


E.g. : I want to copy some number from some internet page and want to paste it inside a inkey textbox ingame.
Posted By: Wjbender

Re: inkey coy paste - 03/30/15 13:16

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>
///////////////////////////////


function main()
{
	vec_set(screen_size,vector(800,400,0));
	vec_set(screen_color,vector(50,1,1)); // dark blue
	vec_set(sky_color,vector(50,1,1)); // dark blue
	video_window(NULL,NULL,0,"My New Game");
	d3d_antialias = 1;
	shadow_stencil = 3;
	
	level_load("");
	vec_set(camera.x,vector(-250,0,50));
	vec_set(camera.pan,vector(0,-15,0));

	 char *text="";
	 if(OpenClipboard(NULL))
	 {
		HANDLE data=GetClipboardData(CF_OEMTEXT);
		if(data!=NULL)
		{
			text=GlobalLock(data);
			GlobalUnlock(data);
		}
		CloseClipboard();
	}

	printf("%s",text);

}



something like this

and

Code:
#include <acknex.h>
#include <default.c>
#include <windows.h>


function main()
{
	level_load("");

	STRING *text="";
	if(OpenClipboard(NULL))
	{
		text=GetClipboardData(CF_OEMTEXT);
		CloseClipboard();
	}

	printf("%s",text);
	
	sys_exit("");

}



this wil work to0,but i think locking is safer?
Posted By: Reconnoiter

Re: inkey coy paste - 03/30/15 15:06

Awesome, thanks alot Wjbender!
Posted By: MasterQ32

Re: inkey coy paste - 03/31/15 00:24

note that you should access the data between the GlobalLock and the GlobalUnlock as only there the data it is guaranteed to exist. Out of the locking structure, the data may be destroyed by another application (windows)
Posted By: Wjbender

Re: inkey coy paste - 03/31/15 11:44

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>
///////////////////////////////


void main()
{
	level_load("");

	STRING *temp="";
	STRING *final="";
	if(IsClipboardFormatAvailable(CF_OEMTEXT))//is this format available ?
	{
		if(OpenClipboard(hWnd))//open the clipboard
		{			
			HANDLE data=GetClipboardData(CF_OEMTEXT);
			if(data!=NULL)//got handle ?
			{
				temp=GlobalLock(data);//lock
				if(temp!=NULL)//got it ?
				{
					final=temp;	
				}
				GlobalUnlock(data);//unlock 
			}
			
			CloseClipboard();//close clipboard
		}
	}
	
	printf("%s",final);
	
	sys_exit("");

}



so the best would be something like this , locking to prevent it being changed by other app's during
the time your accesing it , right

CF_OEMTEXT is for OEM caracter set.
CF_TEXT format is said to be for ANSI text

both CF_TEXT and CF_OEMTEXT ends each line with a carriage return/linefeed combination, and the end is signalled by a null caracter ...(from old c++ help documentation) .

but they convert as needed if I understood correct
Posted By: Reconnoiter

Re: inkey coy paste - 03/31/15 12:05

i haven't tried your latest posted code yet but with the following I got an Error E1516: Invalid memory area, named CAT in the error message. Maybe cause it pastes beyond the string length?


Code:
function inkey_paste()
{
  if(inkey_active) 
  {
  	 char *text="";
	 if(OpenClipboard(NULL))
	 {
		HANDLE data=GetClipboardData(CF_OEMTEXT);
		if(data!=NULL)
		{
			text=GlobalLock(data);
			GlobalUnlock(data);
		}
		CloseClipboard();
	 }
  	 
  	 str_cat((input_txt.pstring)[0], text);
  	 reset(inkeyoptions_pan, SHOW);
  }	
}



The input text just incase:

Code:
TEXT* input_txt =
{
   layer = 31;
   font = calibri18;
   strings = 1;
}

Posted By: Wjbender

Re: inkey coy paste - 03/31/15 13:32

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>
///////////////////////////////

TEXT* input_txt=
{
	layer=31;
	pos_x=100;
	pos_y=100;
	strings=1;
   flags = TRANSLUCENT | SHOW;
}

void main()
{
 warn_level=6;
 
	char *temp="";
	char *final="";
	
 
	if(IsClipboardFormatAvailable(CF_OEMTEXT))//is this format available ?
	{
		if(OpenClipboard(hWnd))//open the clipboard
		{			
			HANDLE data=GetClipboardData(CF_OEMTEXT);
			if(data!=NULL)//got handle ?
			{
				temp=GlobalLock(data);//lock
				if(temp!=NULL)//got it ?
				{
					final=temp;	
				}
				GlobalUnlock(data);//unlock 
			}			
			CloseClipboard();//close clipboard
		}
	}
	
	str_cpy((input_txt.pstring)[0],"");
	str_cat((input_txt.pstring)[0],final);
	//printf("%s",_chr((input_txt.pstring)[0]) );
	
}



looked fine for me , I filled the pointer with str_cpy before using it ..
Posted By: Reconnoiter

Re: inkey coy paste - 03/31/15 15:41

Hmmm, I got the error once more (same one with CAT in it) with your new code. I can't seem to reproduce it easily though, though that is sometimes the case with memory errors. I only seem to have it with this paste function. I checked if there is not something else like a sys_marker that could add the 'CAT' but I have none in my code so I think the CAT is from str_cat.

Maybe it is conflicts with inkey itself?

I will keep an eye out and post here if the error occures again. Also changed the str_cat to str_cpy to see if the CAT changes to CPY or such.
Anyway thanks for the code.
Posted By: MasterQ32

Re: inkey coy paste - 03/31/15 16:31

your pointer is invalid! grin
final points to some invalid memory as soon as you call Unlock!
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>
///////////////////////////////


void main()
{
	level_load("");

	STRING *temp="";
	char final[256]; // max length
	if(IsClipboardFormatAvailable(CF_OEMTEXT))//is this format available ?
	{
		if(OpenClipboard(hWnd))//open the clipboard
		{			
			HANDLE data=GetClipboardData(CF_OEMTEXT);
			if(data!=NULL)//got handle ?
			{
				temp=GlobalLock(data);//lock
				if(temp!=NULL)//got it ?
				{
					// Copy here, don't use reference
					// Also use strcpy and not str_cpy
					// as we work with c-strings, not 
					// Lite-C STRINGS
					strcpy(final, temp;)
				}
				GlobalUnlock(data);//unlock 
			}
			
			CloseClipboard();//close clipboard
		}
	}
	
	printf("%s",final);
	
	sys_exit("");

}

Posted By: Wjbender

Re: inkey coy paste - 03/31/15 16:37

I don't know what's up , I have put the code into a while loop , copied text randomly for an extended period of time , i cannot find the error you get ..

good luck though.

edit : aah master to the rescue !
Posted By: Reconnoiter

Re: inkey coy paste - 03/31/15 17:24

Hi,

I managed to got the error 1 more time but this time it said STR instead of CAT (prob. cause I am using str_cpy now). My code is the same as Master32q's except I added a str_cpy after the c-style strcpy cause I want to copy the result of 'final' to the (input_txt.pstring)[0]. I think str_cpy line creates the error. Here's my code:

Code:
function inkey_paste()
{
  if(inkey_active) 
  {
	 STRING *temp="";
 	 char final[256]; // max length
  	 
	 if (IsClipboardFormatAvailable(CF_OEMTEXT)) //is the format available
	 {
		if (OpenClipboard(hWnd)) //open the clipboard
		{			
			HANDLE data = GetClipboardData(CF_OEMTEXT);
			if (data != NULL) 
			{
				temp = GlobalLock(data); //lock
				if (temp != NULL) //got it?
				{	
					strcpy(final, temp); //c-style string
					////strcpy((input_txt.pstring)[0], final); //instand crash
					str_cpy((input_txt.pstring)[0], final); //mem crash I think 
				}
				GlobalUnlock(data); //unlock 
			}			
			CloseClipboard(); //close clipboard
		}
	 }
  	 
  	 reset(inkeycommands_pan, SHOW);
  }	
}



Perhaps I should replace (input_txt.pstring)[0] with input_str and just do
Code:
strcpy(input_str , final); //c-style

than ?
Posted By: MasterQ32

Re: inkey coy paste - 03/31/15 18:05

no.
Code:
char *temp = ...;

// Copy to an existing STRING
STRING *str;
str_cpy(str, temp);

// Creating a new STRING:
STRING *str = str_create(temp);

// Setting a TEXT member
// If no string is set (should be default case), create one
if((input_txt.pstring)[0] == NULL)
    (input_txt.pstring)[0] = str_create(temp);
else
    str_cpy((input_txt.pstring)[0], temp);

Posted By: Reconnoiter

Re: inkey coy paste - 04/01/15 12:49

It seems to work perfectly now, tnx Masterq32.

Can I ask for 1 more thing?; I am also trying to add a copy to clipboard function, but I don't really understand what to do exactly after emptying the clipboard (something with first allocating memory than using SetClipboardData):

Code:
function inkey_copy()
{
  if(inkey_active) 
  {
  	
	 STRING *temp="";
 	 char final[256]; // max length
  	 
	 if (IsClipboardFormatAvailable(CF_OEMTEXT)) //is the format available
	 {
		if (OpenClipboard(hWnd)) //open the clipboard
		{			
			EmptyClipboard(); //empty clipboard
			
			..... ?			
			
			CloseClipboard(); //close clipboard
		}
	 }
  	 
  	 reset(inkeycommands_pan, SHOW);
  	 
  }	
}

Posted By: Wjbender

Re: inkey coy paste - 04/01/15 18:05

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>
///////////////////////////////

TEXT* input_txt=
{
	layer=31;
	pos_x=100;
	pos_y=100;
	strings=1;
	flags = TRANSLUCENT | SHOW;
}

void from_clipboard()
{
 
	STRING *temp="";
	STRING *final="";
	
	if(IsClipboardFormatAvailable(CF_OEMTEXT))//is this format available ?
	{
		if(OpenClipboard(hWnd))//open the clipboard
		{					
			HANDLE data=GetClipboardData(CF_OEMTEXT);
			if(data!=NULL)//got handle ?
			{
				temp=GlobalLock(data);//lock
				if(temp!=NULL)//got it ?
				{
					final=temp;	
				}
				GlobalUnlock(data);//unlock 
			}			
			CloseClipboard();//close clipboard
		}
	}
	
	if((input_txt.pstring)[0]==NULL)(input_txt.pstring)[0]=str_create(final);
	else str_cpy((input_txt.pstring)[0],final);

}

void to_clipboard(char *content)
{
 
   int len=str_len(content)*sizeof(char);
   if(len==0) return;//data was empty
	
	if(OpenClipboard(hWnd))//open the clipboar
	{
		if(EmptyClipboard())//emptied?
		{ 
			HANDLE mem=GlobalAlloc(GMEM_MOVEABLE,len+1);//alloc mem
			if(mem)//non zero ?
			{
				char* temp=GlobalLock(mem);
				memcpy(temp,content,len);
				if(!SetClipboardData(CF_OEMTEXT,mem))
				{
					GlobalFree(mem);
					CloseClipboard();
					return;
				}
				GlobalUnlock(mem);
				CloseClipboard();
			}
		}
	}
	   

}

void main()
{
 	warn_level=6;
	
	to_clipboard("hello this is a test");
	from_clipboard();
}

Posted By: DLively

Re: inkey coy paste - 04/01/15 19:10

Ever Cool! Did you create that Wjbender?
Posted By: Wjbender

Re: inkey coy paste - 04/01/15 20:26

nah it's just a bunch of Windows functions
Posted By: DLively

Re: inkey coy paste - 04/01/15 20:58

Pretty Sweet! Thanks for sharing this!
Posted By: WretchedSid

Re: inkey coy paste - 04/01/15 21:07

@Wbjender: You are releasing a lock that is used to grant exclusive access to data that you use afterwards! Ie, you are race conditioning it all up in there.
Posted By: Wjbender

Re: inkey coy paste - 04/01/15 21:17

ofcourse I usually screw something up somewhere , what's the point of flawless code lol

https://msdn.microsoft.com/en-us/library/ms649016(VS.85).aspx#_win32_Copying_Information_to_the_Clipboard

I never used the clipboard but
studying msdn example will help though , to whom needs it
Posted By: Wjbender

Re: inkey coy paste - 04/02/15 09:06

Code:
void to_clipboard(char *content)
{
 
   int len=str_len(content)*sizeof(char);
   if(len==0) return;//data was empty
	
	if(OpenClipboard(hWnd))//open the clipboard
	{
		if(EmptyClipboard())//emptied?
		{ 
			HANDLE mem=GlobalAlloc(GMEM_MOVEABLE,len+1);//alloc mem
			if(mem)//non zero ?
			{		
				char* temp=GlobalLock(mem);
				memcpy(temp,content,len);
				GlobalUnlock(mem);		
				SetClipboardData(CF_OEMTEXT,mem);
			}
		}
		CloseClipboard();
	}
}



this follows the msdn example in flow , better ?
Posted By: Ch40zzC0d3r

Re: inkey coy paste - 04/02/15 10:02

Well you forgot to call GlobalFree which might be a big memory leak if the user pastes alot of data. However, just let the people do it theirselves..
Using windows APIs is c&p from msdn. If they cant manage to find something that simple on the net then I have no idea if that helps in any way sorry smirk
Posted By: Wjbender

Re: inkey coy paste - 04/02/15 10:05

I don't want to sound foolish but I can almost swear I have read that the system owns the memory after that and the system wil free it by calling the correct free command , unless you alloc with some owner type of type (forgot the name)

Memory and the Clipboard
A memory object that is to be placed on the clipboard should be allocated by using the GlobalAlloc function with the
GMEM_MOVEABLE flag.
After a memory object is placed on the clipboard, ownership of that memory handle is transferred to the system. When the
clipboard is emptied and the memory object has one of the following clipboard formats, the system frees the memory object
by calling the specified function:
Function to free
object Clipboard format
DeleteMetaFile CF_DSPENHMETAFILE
CF_DSPMETAFILEPICT
CF_ENHMETAFILE
CF_METAFILEPICT
DeleteObject CF_BITMAP
CF_DSPBITMAP
CF_PALETTE
GlobalFree CF_DIB
CF_DIBV5
CF_DSPTEXT
CF_OEMTEXT
CF_TEXT
CF_UNICODETEXT
none CF_OWNERDISPLAY
When the clipboard is emptied of a CF_OWNERDISPLAY object, the application itself must free the
memory object.
Posted By: Reconnoiter

Re: inkey coy paste - 04/02/15 11:04

Thanks Wjbender, works like a charm.

For people who want to send a string as a parameter (like for an inkey copy function), the following seems to work (though I am not sure if it is flawless code):

Code:
function inkey_copy()
{
  char* temp_char;
  temp_char = _chr(input_str);
  to_clipboard(temp_char);	
}

© 2024 lite-C Forums