CHDK Manual Focus capability has been greatly improved starting with version 1.3.0. The rest of this wiki page describes CHDK 1.2.0 or earlier.

forum thread -> Manual Focus Testing Thread


Out of focus

One of the very useful features of CHDK is its ability to focus the camera manually,  either as part of a script or through the CHDK menus.  While some Canon cameras have a built-in manual focus options, many do not.  And for all cameras, the ability to set a focus point in a script can be important, either for "focus stacking" shots or to force the camera to focus at infinity (or the hyper-focal point) when shooting with intervalomters.

However,  determining how to use CHDK to reliably manually focus a camera's lens can be difficult as many cameras need to be configured a little differently.   This applies to both the "set_focus()" command in CHDK scripts,  or the "Override Subject Distance" menu item in the CHDK's Enhanced Photo Operations menu. This problem is caused by there being no standard method that works for every camera that allows you to enable manual focus using CHDK.

Some cameras will immediately focus whenever a set_focus() script command is issued or SD override via menu..

Others will only focus when the camera's built-in autofocus is locked (AFL) prior to a set_focus() or SD override via menu.

And still others need the camera to be in Canon manual focus (MF) mode before you can focus via CHDK set_focus() or SD override via menu.

Listed below are some notes collected from various CHDK forum posts and wiki pages that should help you determine how to enable manual focus via CHDK for your camera. A Lua script is also given to simply determine what works for a particular camera.

Enabling CHDK Manual Focus[]

The following instructions will show you how to enable CHDK's ability to manually focus (either in a script or via the CHDK SD Override menu item).

1) Can CHDK manually focus with no changes to the Canon setting ? To test this .... If this works, you are done. Congratulations! Otherwise go to step 2 below.

2) Does your camera natively support manual focus using the Canon menus ? If so ..

3) Will your camera allow CHDK to manually focus only when it is Canon AFL mode?

4) Will your camera allow a CHDK script to manually focus when CHDK issues the set_aflock() command ?

CHDK Manual Focus Scripting Commands[]


  • set_aflock(1) causes the Canon firmware to stop focusing when shooting
  • does an initial focus measurement and sets focus at that value when called
  • set_aflock(0) restores camera automatic focusing.
  • seems to works on almost all cameras
  • using set_aflock() may be necessary on some camera for set_focus() to work (elph310). On other cameras, it has no effect on whether set_focus() works.
  • on some cameras,  the set_aflock() command must be issued after a "half-press" focus completes and before the half press is released
  • if you call set_aflock() and then set_focus() (in a mode on your camera where that works) then the focus for all subsequent shots will stay at the value specified in set_focus().  If set_aflock() has not been called then set_focus() will only apply to the next shot - after that the camera will revery to auto focus.


  • script command that may or may not actually set the focus setting (in mm) the next time the camera shoots.
  • on some cameras, set_focus() will only work if the camera is in Canon AFL mode (ex: A1200)
  • on some cameras, set_focus() will only work if the camera is in Canon MF mode (ex: SX50HS)
  • on some cameras, set_focus() will work when set_aflock(1) has been called (ex: elph310)
  • on some cameras, set_focus() will only work when the camera is in P mode (or at least not in AUTO)
  • on some cameras, set_focus works every time regardless of mode (ex: G10, SD940)
Notes :
  1. Canon AFL mode is typically achieved via simulated button presses using click() - see your camera manual for the correct sequence. May require holding a "half press" while another key is clicked.
  2. Canon MF mode is may be achieved via simulated button presses, post_levent_for_npt call, and/or call_event_proc calls.
  3. When in Canon MF mode,  focus will be set immediately when a set_focus() command is issued.  Otherwise the focus will not take effect until after the "half-press" part of the shooting sequence.
  4. If all of the above fail to work, you may be able to set focus to infinity on any camera by using click() commands to select the "landscape" focus mode.  The needed button is usually indicated by the tulip flower icon on one of the camera buttons but check your camera manual to be sure.

Forum Links[]

Test Script[]

link >  Test Results posted here

The script copied below lets you try out the various options for using set_focus().   Save it to an ASCII plain text file with the name mf-test.lua.

The script provides six choices of camera setup - pick one from the user parameter at run time to see if that mode will work for your camera.  

The script will attempt to focus at 100, 200, and 300 mm and take a two test shots at each distance.  The first shot is preceeded by a set_focus() command but the second shot is not.   The purpose of the second shot is to see if the focus distance set for the first shot is preserved for the second shot.

@title MF Test Suite
   @param   f MF Mode
   @default f 0
   @values  f None AFlock AFL MF1 MF2 MF3
set_console_layout(1 ,1, 45, 14 )
print( "Set Focus Tester v1.2")
if ( get_mode() == false ) then
    while ( get_mode() == false ) do sleep(100) end

if    ( f==1) then              -- method 1 : set_aflock()
    print("set_aflock() mode")
elseif( f==2) then              -- method 2 : enable Canon AFL lock focus via key presses 
    print("AFL mode")
    local count = 0
    local timeout = false
        count = count + 1
        if (count > 40 ) then timeout = true end        
    until (get_focus_ok() == 1 ) or (timeout == true) 
    if (timeout == true) then print("Warning : focus attempt timed out")
    else  click("left")
elseif( f==3) then             -- method 3 : MF1 lock focus via key presses 
    print("MF1 mode")
elseif( f==4) then             -- method 4 : MF2 lock focus using levent for key press
    print("MF2 mode")
elseif( f==5) then             -- method 5 : MF3 lock focus using event_proc for key press  
    print("MF3 mode")
else print("no focus mode selected")

for focus=100,300,100  do
    if ((focus-after)/5 == 0) then fstr=" : okay" else fstr=" : fault" end
    print("Set:"..focus," Before:"..before," After:"..after..fstr)
    if ((focus-after)/5 == 0) then fstr=" : locked" else fstr=" : unlocked" end
    print("    "..focus," Before:"..before," After:"..after..fstr)

if     (f == 1 ) then           -- method 1 : aflock disable
elseif (f == 2 ) then           -- method 2 : deselect AFL
elseif (f == 3 ) then           -- method 3 : MF1 off
elseif (f == 4 ) then           -- method 4 : MF2 off
elseif (f == 5 ) then           -- method 5 : MF3 off

print(".. done")

Links to MF Scripts[]

Focus and DOF Stacking

For Developers :  SD Override / Manual Focus[]


This section under active construction as of Dec 29 2013.

To enable SD overrides to work via the CHDK menus, the following default settings in include/camera.h related to manual focus must be configured correctly :

#define CAM_CAN_SD_OVER_NOT_IN_MF       1   // Camera allows subject distance (focus) override when not in manual focus mode
#undef  CAM_CAN_SD_OVER_IN_AF_LOCK          // Camera allows subject distance (focus) override when in AF Lock mode
#undef  CAM_CAN_SD_OVER_IN_AF_LOCK_ONLY     // Camera allows subject distance (focus) override only when in AF Lock mode OR in movie mode
#define CAM_CAN_SD_OVERRIDE             1   // Camera allows to do subject distance override
#define CAM_HAS_MANUAL_FOCUS            1   // Camera has manual focus mode

Use these as follows :