Skip to content

Instantly share code, notes, and snippets.

@tehshane
Last active March 9, 2024 05:18
Show Gist options
  • Save tehshane/8765405 to your computer and use it in GitHub Desktop.
Save tehshane/8765405 to your computer and use it in GitHub Desktop.
AutoHotKey script that remaps the F1-F12 function keys on the Microsoft Sculpt Mobile keyboard to be pressed without having to use the Fn key. Disables some of the Windows 8 shortcuts, but leaves media keys intact.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#InstallKeybdHook
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; OPTIONAL: For those who use Home/End more than PgUp/PgDown, this flips their use with the Fn key.
; If you want the buttons to function as they are, add a semicolon (;) to the beginning of each line below.
Home::PgUp
End::PgDn
PgUp::Home
PgDn::End
; F1 key by default, Fn+F1 is now Play/Pause
MEDIA_PLAY_PAUSE::F1
F1::MEDIA_PLAY_PAUSE
; F2 key by default, Fn+F2 is now Mute/Unmute
VOLUME_MUTE::F2
F2::VOLUME_MUTE
; F3 key by default, Fn+F3 is now Volume Down
VOLUME_DOWN::F3
F3::VOLUME_DOWN
; F4 key by default, Fn+F4 is now Volume Up
VOLUME_UP::F4
F4::VOLUME_UP
; F5 key by default, Fn+F5 does nothing
SendInput {F5}
+#F21::
return
; F6 key by default, Fn+F6 does nothing
!#F21::
SendInput {F6}
return
; F7 key by default, Fn+F7 does nothing
^#F21::
SendInput {F7}
return
; F8 key by default, Fn+F8 does nothing
#F21::
SendInput {F8}
return
; F9 key by default, Fn+F9 does nothing
^#BS::
SendInput {F9}
return
; F10 key by default, Fn+F10 does nothing
^#TAB::
SendInput {F10}
return
; F11 key by default, Fn+F11 does nothing
!#F22::
SendInput {F11}
return
; F12 key by default, Fn+F12 does nothing
^#F23::
SendInput {F12}
return
@Adrianrff
Copy link

Adrianrff commented Feb 27, 2018

This is awesome, I have a question though, what do the+#F21::, ^#BS::, etc mean after each assignment? Also, when testing a keyboard tester, pressing keys from F5 through F12 also show the ctrl (both), shift, and Lwin keys being pressed. However, using the fn button and an F key shows only the F key being pressed. Is the script not working for me? Thanks

@g048406
Copy link

g048406 commented Jul 26, 2018

@DawnSaviors: Just add this to the script (remember to delete lines 25-37 as this will replace the F4, F5 and F6 keys). PS: are you sure you want F4 to adjust the brightness? the F4 in the above script increases the volume):

F4:: AdjustBrightness(-1) ; decrease gamma value by 1 (-1)
F6:: DisplaySetBrightness(128) ; set gamma to standard value (128)
F5:: AdjustBrightness(+1) ; increase gamma value by 1 (+1)

; ===============================================================================================================================

AdjustBrightness(V = 0)
{
SB := (SB := DisplayGetBrightness() + V) > 255 ? 255 : SB < 0 ? 0 : SB
DisplaySetBrightness(SB)
}

DisplaySetBrightness(SB := 128)
{
loop % VarSetCapacity(GB, 1536) / 6
NumPut((N := (SB + 128) * (A_Index - 1)) > 65535 ? 65535 : N, GB, 2 * (A_Index - 1), "UShort")
DllCall("RtlMoveMemory", "Ptr", &GB + 512, "Ptr", &GB, "Ptr", 512)
DllCall("RtlMoveMemory", "Ptr", &GB + 1024, "Ptr", &GB, "Ptr", 512)
return DllCall("gdi32.dll\SetDeviceGammaRamp", "Ptr", hDC := DllCall("user32.dll\GetDC", "Ptr", 0), "Ptr", &GB), DllCall("user32.dll\ReleaseDC", "Ptr", 0, "Ptr", hDC)
}

DisplayGetBrightness(ByRef GB := "")
{
VarSetCapacity(GB, 1536, 0)
DllCall("gdi32.dll\GetDeviceGammaRamp", "Ptr", hDC := DllCall("user32.dll\GetDC", "Ptr", 0), "Ptr", &GB)
return NumGet(GB, 2, "UShort") - 128, DllCall("user32.dll\ReleaseDC", "Ptr", 0, "Ptr", hDC)
}

@0xJes
Copy link

0xJes commented Nov 27, 2018

Is there a workaround like this but for linux/X11 ?

xmodmap can only remap from F1 to F4...

Copy link

ghost commented Nov 14, 2019

Is there a workaround like this but for linux/X11 ?

xmodmap can only remap from F1 to F4...

Why so. If I remember correctly, you can use xev to find keycode and manually unbind and rebind them

@D2Joy
Copy link

D2Joy commented Feb 22, 2020

really help with my anti-human microsoft keyborad, F1-F4 definitely work, just cannot customize F5-F12. Seems Fn does't have a scan code according to web discussions.

@gfrk
Copy link

gfrk commented Oct 8, 2020

For those who don`t know how to code: download this program: http://ahkscript.org/
Then follow this tutorial to se how to use it: https://www.youtube.com/watch?v=lxLNtBYjkjU
Then, copy-paste the code above in the new fahkscript file, save the file, run it and wallahh...
It works and it´s solve in less than 5 minutes!!
Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment