View Full Version : saveing to a file
Hi, there
I was wondering how could i use keys that are press, to be exported to a file
i cant seem to get it right to let is save :-[
Michael Hartlef
09-09-2007, 09:21
Hi Henry and welcome here!
Can you post your script? That would make it much easier to help you.
ErosOlmi
09-09-2007, 09:45
Welcome Henry,
I would like to help you but I cannot understand your request.
Can you please give us more info? Maybe your request can bring new functionalities to File module.
There are already some File keywords I would like to rewrite.
Let us know.
Ciao
Eros
i would like to know how to save E.g. using input from keys
getasynckeystate(%vk_return)
then exporting that key if it was press into a file or text file
ErosOlmi
09-09-2007, 10:01
OK,
now I got it.
GetAsyncKeyState (http://www.thinbasic.com/public/products/thinBasic/help/html/getasynckeystate.htm) function does not return any interesting info other than %TRUE or %FALSE if the indiciated key passed as parameter has been pressed.
What you can do is to store the key equate in a sequential buffer and than save that buffer into a file.
Give me some minutes and I can see if I can produce a little script.
Are you creating a keylogger or something like that?
Ciao
Eros
not really i just want to have a understanding of tbasic and saving inputs from the computer e.g. the keys
ErosOlmi
09-09-2007, 10:20
Henry,
the following is a rough example. A lot of changes should be applied like intercepting a key that remain pressed or avoid to get the same key if it was pressed in the last x milliseconds, and so on. In any case it can give you a start.
Every time you press a key the full set of possible keys are scanned and if one (or more) is pressed it will add the key number to a string buffer. You can save the string buffer clicking on the button.
It is very basic but, you know, also complex things start from a basic example ;D
Let me know.
Eros
uses "UI" 'use UI module
uses "File"
dim gBuffer as string
DIM hDlg AS DWORD 'dialog number assigned by OS
dim Msg, wparam,lparam as dword 'parameters used for os messages
%id_button = 10 'assign number to button to identify it
%id_Text = 20 'assign number to text to identify it
'make a window
DIALOG NEW 0, "Press some key. At the end click button to have text file", -1, -1, 350, 300, _
%WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, _
0 TO hDlg
'add a button to it
control add button , hDlg, %id_button, "Click me to stop and save", 5, 5, 100, 30, %ws_border
control add textbox, hDlg, %id_Text , "" , 5, 50, 340, 200, %ws_border or %ES_AUTOVSCROLL or %ES_MULTILINE
DIALOG SHOW modeless hDlg 'make window visible
while isWindow(hDlg) 'while loop to handle messages from OS
Msg = GetMessage(hDlg, wParam, lParam) 'get latest message
select case Msg 'message that was recieved
case %wm_command 'check command
select case wParam 'it will be here
case %id_button 'was button clicked?
file_save(app_sourcepath & "KeyPressed.txt", gBuffer)
exit while
end select
CASE %WM_SYSCOMMAND 'system command?
SELECT CASE wParam 'check variable
CASE %SC_CLOSE 'if close button in bar clicked then
EXIT WHILE 'exit while which will end program
END SELECT
END SELECT
GetKeyPressed
wend
DIALOG END hDlg 'ends the program
function GetKeyPressed() as long
dim Counter as long
for Counter = 1 to 255
if getasynckeystate(Counter) then
gBuffer += iif$(gBuffer = "", "", ",") & str$(Counter)
control set text hDlg, %ID_Text, gBuffer
end if
next
end function
thanks man ill check it out
ErosOlmi
09-09-2007, 10:46
Henry,
I've added a little bit intelligence to the script. In this version an array of bytes store the change of the key state so it can understand when key is pressed or when key is released. Hope this can help.
Ciao
Eros
uses "UI" 'use UI module
uses "File"
dim gBuffer as string
dim vKeys(255) as byte
DIM hDlg AS DWORD 'dialog number assigned by OS
dim Msg, wparam,lparam as dword 'parameters used for os messages
%id_button = 10 'assign number to button to identify it
%id_Text = 20 'assign number to text to identify it
'make a window
DIALOG NEW 0, "Press some key. At the end click button to have text file", -1, -1, 350, 300, _
%WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, _
0 TO hDlg
'add a button to it
control add button , hDlg, %id_button, "Click me to stop and save", 5, 5, 100, 30, %ws_border
control add textbox, hDlg, %id_Text , "" , 5, 50, 340, 200, %ws_border or %ES_AUTOVSCROLL or %ES_MULTILINE
DIALOG SHOW modeless hDlg 'make window visible
while isWindow(hDlg) 'while loop to handle messages from OS
Msg = GetMessage(hDlg, wParam, lParam) 'get latest message
select case Msg 'message that was recieved
case %wm_command 'check command
select case wParam 'it will be here
case %id_button 'was button clicked?
file_save(app_sourcepath & "KeyPressed.txt", gBuffer)
exit while
end select
CASE %WM_SYSCOMMAND 'system command?
SELECT CASE wParam 'check variable
CASE %SC_CLOSE 'if close button in bar clicked then
EXIT WHILE 'exit while which will end program
END SELECT
END SELECT
GetKeyPressed
wend
DIALOG END hDlg 'ends the program
function GetKeyPressed() as long
dim Counter as long
dim UpdateTextBox as long
for Counter = 1 to 255
if getasynckeystate(Counter) then
if vKeys(Counter) = 1 then
'---It was already pressed so do nothing
else
'---Set vKeys to 1 so we know it has been pressed
vKeys(Counter) = 1
gBuffer += iif$(gBuffer = "", "", ",") & str$(Counter) & "(Pressed)"
UpdateTextBox = %TRUE
end if
else
if vKeys(Counter) = 0 then
'---It was already not pressed so do nothing
else
'---Set vKeys to 0 so we know it has been released
vKeys(Counter) = 0
gBuffer += iif$(gBuffer = "", "", ",") & str$(Counter) & "(Released)"
UpdateTextBox = %TRUE
end if
end if
next
if UpdateTextBox = %TRUE then control set text hDlg, %ID_Text, gBuffer
end function
ErosOlmi
09-09-2007, 10:49
Forgot to say that I've created a window in the script just for convenience and to have a textbox where to show results but getasynckeystate works even if no window is created. You just need to have loop where to check keys.
Ciao
Eros
ah thanks is there a way to make it so it records without havening to interact
ErosOlmi
09-09-2007, 11:34
Attached to this post please find a no-interface version of the script.
Thanks ;D let say i want to make it so i can decode it would i set E.G. if it was 1 would i can set it as mouse pressed?
ErosOlmi
09-09-2007, 11:50
Well, trapping the keyboard is quite easy.
Trapping the mouse and/or the exact sequence user does is not trivial and windows message pump plus internal windows journal is to keep in then "game".
Not sure I can do this right now. Those applications are quite complex and require the injection of specific function in window messages stacks. Sorry.
In any case I will think about it.
Eros
thanks or i could do is check each key is there a way to find out easer ?
ErosOlmi
09-09-2007, 12:03
This is the Microsoft reference to GetAsyncKeyState function: http://msdn2.microsoft.com/en-us/library/ms646293.aspx
And the following is the list of virtual keys: http://msdn2.microsoft.com/en-us/library/ms645540.aspx
Hope this will help.
Ciao
Eros
thanks man this helped me alot :) hope you enjoy your day:)
Eros you got the working or start to a nice Macro Recorder it seems :)
ErosOlmi
09-09-2007, 22:52
:D
Working with keyboard is quite easy. Much more difficult is to trap, store and play mouse events.
Put together:
GetAsyncKeyState (http://www.thinbasic.com/public/products/thinBasic/help/html/getasynckeystate.htm)
SendKeys (http://www.thinbasic.com/public/products/thinBasic/help/html/sendkeys.htm)
Win_FindByTitle (http://www.thinbasic.com/public/products/thinBasic/help/html/win_findbytitle.htm)
Win_SetForeground (http://www.thinbasic.com/public/products/thinBasic/help/html/win_setforeground.htm)
and you can control any window keyboard input.
Ciao
Eros