Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

turning off and on a program

by Mattk470 (Novice)
on Dec 13, 2006 at 16:36 UTC ( [id://589610]=perlquestion: print w/replies, xml ) Need Help??

Mattk470 has asked for the wisdom of the Perl Monks concerning the following question:

I'm using this script to write anything copied to the clipboard to a file however I want to be able to turn it off/on using 'Ctrl-whatever' and do other options in the program as well (ie.. When I hit ctrl-a then write to file 'A'; when I hit ctrl-b then write to file 'B'; ctrl-c, popup a cmd window for entering text etc.). I'm on WinXP and its running from start-up and in the background using wperl.
use strict; use warnings; use Win32::Clipboard; my $outputfile = 'c:\perl\log.txt'; my $clipboard = Win32::Clipboard(); $clipboard->Empty(); while (1) { $clipboard->WaitForChange(); open (OUT, ">>$outputfile") || die "Could not open $outputfile"; my $text = $clipboard->GetText(); print OUT $text,"\n\n"; print $text,"\n\n"; close OUT; } exit(0);

Replies are listed 'Best First'.
Re: turning off and on a program
by Albannach (Monsignor) on Dec 14, 2006 at 05:28 UTC
    For what it's worth, here's a quick-and-dirty option for consideration: rather than having one program watching the clipboard all the time, have several similar programs that each carry out one simple task and immediately exit. Then you link each to the desired keystrokes through Windows shortcuts (each being Ctrl-Alt-<letter>). This may well present some delay for each actuation depending on your hardware and load, but it should work and avoids having to filter through every keystroke in the system just in case it is meant for your application. This does work, but whether it will be responsive enough for your needs can only be answered by a test.

    --
    I'd like to be able to assign to an luser

Re: turning off and on a program
by geekphilosopher (Friar) on Dec 13, 2006 at 21:37 UTC

    If this program is running in the background (i.e. does not have focus), you'll need to somehow intercept all keypresses, passing them along to the operating system unless they're one of your control sequences.

    I'm not entirely sure how one would do this - there may be no easy way. You should browse through the Windows modules on CPAN to see if you can find anything useful.

Re: turning off and on a program
by ldln (Pilgrim) on Dec 14, 2006 at 09:55 UTC
    Check out Continueing the Quest to get RegisterHotKey to work!
    #This works! We have global keyboard hook for hotkey, ctrl+a. use strict; use warnings; use Win32::GUI; use Win32::API; use Win32::API::Callback; $| = 1; # From PlatformSDK/Include/WinUser.h use constant MOD_CONTROL => 2; use constant KEY_A => ord('A'); use constant MY_HOTKEY_ID => 99; use constant { # Codes for SetWindowHook WH_GETMESSAGE => 3, WH_KEYBOARD => 2, # Window Messages WM_HOTKEY => 0x0312, ### # Hook Codes ### HC_ACTION => 0, ### HC_GETNEXT => 1, ### HC_SKIP => 2, ### HC_NOREMOVE => 3, ### HC_SYSMODALON => 4, ### HC_SYSMODALOFF => 5, ### ### # PeekMessage() Options ### PM_NOREMOVE => 0x0000, ### PM_REMOVE => 0x0001, ### PM_NOYIELD => 0x0002, }; sub WindowProc { my ( $nCode, $wParam, $lParam ) = @_; # See http://msdn.microsoft.com/library/en-us/winui/winui/windowsu +serinterface/windowing/messagesandmessagequeues/messagesandmessageque +uesreference/messageandmessagequeuestructures/msg.asp # typedef struct { # HWND hwnd; # UINT message; # WPARAM wParam; # Unsigned # LPARAM lParam; # Signed # DWORD time; # POINT pt; # } MSG, *PMSG; my ( $msg_hwnd, $msg_message, $msg_wParam, $msg_lParam, $msg_time, + $msg_pt ) = unpack 'L L L l L L ', unpack( 'P24', pack('L', $lParam) ); # print "\n\nTime:", $msg_time; # print "\nMsg_lparam", $msg_lParam; #Hotkey code to run if ( $msg_message == WM_HOTKEY ) { #print "\a**************************\n"; system("notepad.exe"); } print "\nMessage:", $msg_message; CallNextHookEx( 0, $nCode, $wParam, $lParam ); } my $WinProc = Win32::API::Callback->new( \&WindowProc, 'NNN', 'N' ); Win32::API->Import( kernel32 => GetCurrentThreadId => '', 'N' ) or + die; Win32::API->Import( user32 => SetWindowsHookEx => 'NKNN', 'N' ) or + die; Win32::API->Import( user32 => CallNextHookEx => 'PNNN', 'N' ) or + die; Win32::API->Import( user32 => RegisterHotKey => 'NNNN', 'N' ) or + die; Win32::API->Import( user32 => UnregisterHotKey => 'NN', 'N' ) or + die; my $ThreadId = GetCurrentThreadId() or die; print "\nThreadID:", $ThreadId; SetWindowsHookEx( WH_GETMESSAGE, $WinProc, 0, $ThreadId ) or die $^E; #SetWindowsHookEx( WH_KEYBOARD, $WinProc, 0, $ThreadId ) or die $^E; my $mw_win32 = Win32::GUI::Window->new( -name => 'MainWindow', -title => 'This is a test!', -width => 100, -height => 100, -pos => [ -400, -400 ] ); $mw_win32->Show(); $mw_win32->Hide(); my $mw_handle = $mw_win32->GetActiveWindow(); RegisterHotKey( $mw_handle, MY_HOTKEY_ID, MOD_CONTROL, KEY_A ) or die +$^E; Win32::GUI::Dialog(); # print "done"; UnregisterHotKey( $mw_handle, MY_HOTKEY_ID ) or die $^E;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://589610]
Approved by andyford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found