AutoHotkey Community

It is currently May 27th, 2012, 7:50 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: October 15th, 2006, 7:39 am 
I saw a handy script posted in the BF script topic but I have tried to use it with out much luck. The script was the code I have added the header but when I run it in BF2 1.4 under windows XP SP2 and press F nothign happens. If any one could give me a few tips or tell me what I am doing wrong that would be great.

Code:
;
; AutoHotkey      Version: 1.x
; Language:       English
; Platform:       Win9x/NT
;
;
; Only run this script when bf2.exe is running in the foreground
;
#SingleInstance force
#InstallKeybdHook
#InstallMouseHook


SetTimer, KeepRunning
return

KeepRunning:

  ; Get the process name of the active window (i.e. Notepad.exe)
  WinGet, szProcessName, ProcessName, A
  if szProcessName = bf2.exe
  {
    Suspend, off
  }
  else
  {
    Suspend, on
  }
return

;
; Disable the Window's keys so they don't switch to desktop while in-game
;
$LWin::   ; Left Windows Button
$RWin::   ; Right Windows Button
   ; Do nothing
return

$^CapsLock::
   ExitApp
return

SetKeyDelay, 50, 50                  ; NB: Once "WeaponSelect()" added this was needed!!

v_LastUsedWeapon = 3               ; Initialise as Primary Weapon 3
v_LastStoredWeapon = 3               ; Initialise as Primary Weapon 3

$f::Goto, QuickSwitch
$1::WeaponSelect(1)
$2::WeaponSelect(2)
$3::WeaponSelect(3)
$4::WeaponSelect(4)
$5::WeaponSelect(5)
$6::WeaponSelect(6)

KeyPress(v_KeyName)
{
  Send, {%v_KeyName% Down}
  Sleep %KeySleep1%
  Send, {%v_KeyName% Up}
  If( v_KeyName > 0 and v_KeyName < 7)      ; QuickSwitch: Check if weapon, between 1-8, should it be 1-6?
  ; NB: Above 'If' only needed if Quickswitch desired.
  {
    v_ChosenWeapon = %v_KeyName%
    QuickSwitchVar(ByRef v_ChosenWeapon)
  }
}

QuickSwitch:
  KeyPress(ByVar v_LastUsedWeapon)         ; QuickSwitch: "v_LastUsedWeapon" is declared globally and stored by "QuickSwitchVar()" function
return

QuickSwitchVar(v_ChosenWeapon)
; 1.Edit Store previous weapon, 2. Placeholder variable for current weapon so next use of function can determine previous weapon
{
  Global v_LastUsedWeapon
  Global v_LastStoredWeapon
  v_LastUsedWeapon = %v_LastStoredWeapon%   ; QuickSwitch: Previous run stored source for this variable. Used to record last used Weapon and used in QuickSwitch usage
  v_LastStoredWeapon = %v_ChosenWeapon%      ; QuickSwitch: Stored so the following call of function can determine Previous Weapon
}

WeaponSelect(v_ChosenWeapon)
; Select weapon 1 - 6 %v_ChosenWeapon%
{
  If( v_ChosenWeapon > 0 and v_ChosenWeapon < 7)
  {
    Global v_LastStoredWeapon               ; QuickSwitch: Declare this to to access it and determine previous weapon
    If( v_ChosenWeapon <> v_LastStoredWeapon)   ; QuickSwitch: If current weapon wasn't pressed last time then ...
      QuickSwitchVar(ByRef v_ChosenWeapon)      ; ... update variables for weapon history.
    Send, {%v_ChosenWeapon% Down}
    Sleep %KeySleep1%
   Send, {%v_ChosenWeapon% Up}
  }
  Else
    Send, @huh????%v_ChosenWeapon%
  Sleep 50
}





Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 15th, 2006, 8:29 am 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
There are some curious statements:
Code:
  If( v_KeyName > 0 and v_KeyName < 7)      ; QuickSwitch: Check if weapon, between 1-8, should it be 1-6?
  ; NB: Above 'If' only needed if Quickswitch desired.
  {
    v_ChosenWeapon = %v_KeyName%
    QuickSwitchVar(ByRef v_ChosenWeapon)
  }

QuickSwitch:
  KeyPress(ByVar v_LastUsedWeapon)     
return
...
      QuickSwitchVar(ByRef v_ChosenWeapon)
...
    Sleep %KeySleep1%


ByRef is used in function declarations, not in function calls.

Delete ByRef/Byvar and assign some value to KeySleep1. May help!

P.S.: Use a nick or beware of the BoBo!!!

_________________
nick :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 15th, 2006, 8:44 am 
Offline

Joined: October 15th, 2006, 8:41 am
Posts: 10
Opps sorry BoBo :P

will try and give it a shot is there a way to debug code with out running BF2 could I change BF2.exe and put notepad.exe or is there a nice tool to capture and see what is being pressed eg if it pressed Shift and Crtl you would not see this in notepad


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 15th, 2006, 9:18 am 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
I tried your code with if szProcessName = PSPad.exe
For "debugging" I use ListVars, Pause and MsgBox. :wink:

_________________
nick :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 15th, 2006, 2:14 pm 
Offline

Joined: March 24th, 2006, 4:15 pm
Posts: 20
Jackthehat wrote:
Opps sorry BoBo :P

will try and give it a shot is there a way to debug code with out running BF2 could I change BF2.exe and put notepad.exe or is there a nice tool to capture and see what is being pressed eg if it pressed Shift and Crtl you would not see this in notepad


I replied to your PM, thanks for expressing an interest in the idea. The original code I posted might have been missing some variables or such when I copied and pasted so I went back to my original and extracted all the QuickSwitch related pieces and tried them out in a Standalone script. Tested it in Notepad first and then fired up BF2 in singleplayer and the QuickSwitch is working ok so hope you find the below useful.

Code:
;GameName = notepad.exe               ; use when testing in notepad
GameName = bf2.exe                  ; Set exe name here
KeySleep1 = 100                     ; Ideal Sleep time for BF2 commands (check?, SetKeyDelay influence?)
v_InTextChatOn := False               ; Used for pausing running of Script
v_LastUsedWeapon = 3               ; Initialise as Primary Weapon 3
v_LastStoredWeapon = 3               ; Initialise as Primary Weapon 3

#Persistent
#NoEnv                           ; prevents empty variables from being looked up
#SingleInstance force
#InstallKeybdHook
#InstallMouseHook
SetKeyDelay, 75, 75                  ; NB: Once "WeaponSelect()" added this was needed!!
SetTimer, KeepRunning               ; Only run this script when {BF2.exe} is running in the foreground
Return

;;;; Timer related ;;;;
KeepRunning:
  ; Get the process name of the active window (i.e. Notepad.exe)
  ; Only run this script when Battlefield 2 {BF2.exe} is running in the foreground.
  WinGet, szProcessName, ProcessName, A
  If szProcessName = %GameName%         ; Determine if window Active, enable hotkeys
  {
    ; If running check to determine whether in chat mode.
    If (v_InTextChatOn == False)      ; Text chat isn't active so allow hotkeys
    {
      Suspend, off
    }
    Else if (v_InTextChatOn == True)   ; Text chat is active so disable hotkeys
   {
     Suspend, on
   }
  }
  Else                           ; Else window inactive, disable hotkeys
  {
    Suspend, on
  }
Return


; --------------------

; Quickswitch related.
$1::WeaponSelect(1)
$2::WeaponSelect(2)
$3::WeaponSelect(3)
$4::WeaponSelect(4)
$5::WeaponSelect(5)
$6::WeaponSelect(6)    
$f::Goto, QuickSwitch 

QuickSwitch:
  KeyPress(ByVar v_LastUsedWeapon)         ; QuickSwitch: "v_LastUsedWeapon" is declared globally and stored by "QuickSwitchVar()" function
return

QuickSwitchVar(v_ChosenWeapon)
; 1.Edit Store previous weapon, 2. Placeholder variable for current weapon so next use of function can determine previous weapon
{
  Global v_LastUsedWeapon
  Global v_LastStoredWeapon
  v_LastUsedWeapon = %v_LastStoredWeapon%   ; QuickSwitch: Previous run stored source for this variable. Used to record last used Weapon and used in QuickSwitch usage
  v_LastStoredWeapon = %v_ChosenWeapon%      ; QuickSwitch: Stored so the following call of function can determine Previous Weapon
}

WeaponSelect(v_ChosenWeapon)
; Select weapon 1 - 6 %v_ChosenWeapon%
{
  If( v_ChosenWeapon > 0 and v_ChosenWeapon < 7)
  {
    Global v_LastStoredWeapon               ; QuickSwitch: Declare this to to access it and determine previous weapon
    If( v_ChosenWeapon <> v_LastStoredWeapon)   ; QuickSwitch: If current weapon wasn't pressed last time then ...
      QuickSwitchVar(ByRef v_ChosenWeapon)      ; ... update variables for weapon history.
    Send, {%v_ChosenWeapon% Down}
    Sleep %KeySleep1%
   Send, {%v_ChosenWeapon% Up}
  }
  Else
    Send, @huh????%v_ChosenWeapon%
  Sleep 50
}

KeyPress(v_KeyName)
{
  Send, {%v_KeyName% Down}
  Sleep %KeySleep1%
  Send, {%v_KeyName% Up}
  If( v_KeyName > 0 and v_KeyName < 7)      ; QuickSwitch: Check if weapon, between 1-8, should it be 1-6 as NightVision and gas Mask not really need in the switch?
  ; NB: Above 'If' only needed if Quickswitch desired.
  {
    v_ChosenWeapon = %v_KeyName%
    QuickSwitchVar(ByRef v_ChosenWeapon)
  }
}

; --------------------


When I originally thought of I thought it would be quiet simple but of course as I delved in a little deeper it became a little more complex, after some trial and error in Notepad the code evolved and the above appears to work fine. Someone a little more expereinced with coding might be able to simplify it further. Anyway hope someone finds it useful.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2006, 11:25 am 
Offline

Joined: October 15th, 2006, 8:41 am
Posts: 10
Works perfect thank you very much do you think you could add this script to the BF2 offical thread as its a very handy script with some quite advanced options.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2006, 11:34 am 
Jackthehat wrote:
Works perfect thank you very much do you think you could add this script to the BF2 offical thread as its a very handy script with some quite advanced options.


When I posted this here I edited my code in the original posting so anyone interested in it should be good to go. Glad you find it useful.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: bobbysoon, HotkeyStick, just me and 68 guests


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

Search for:
Powered by phpBB® Forum Software © phpBB Group