How to emulate Fn (function key) keypress in xte

6,421

It is usually very tricky or near impossible to use xte to simulate fn + f key keypresses, as the fn key itself does not register at all in xev, and very often the fn keys or fn combinations are not seen by the kernel like other normal keyboard keys.

Summary

I actually managed to setup three key combinations (fn + f1, fn + f2 and fn + f3), because pressing each of these combinations resulted in the kernel issuing its 'unknown key pressed...set keycodes' dialogue. This meant that I was able to set the keycodes, and then go to the keyboard shortcuts dialogue and see if a keysym was assigned, e.g. XF86Paste. If so, it was now possible to assign a program to launch from them and it was also now possible to use xte to issue the following command, which was the equivalent of pressing fn + f1 for me:

xte 'keydown XF86Paste' 'keyup XF86Paste'

It may be a pecularity of one of my laptops that I can set three of these fn combinations like normal multimedia keys as shown here, but I'll explain the process anyway for you and the particular steps that I followed.


Full Explanation

You might not be able to set the keys, as usually you cannot, but two or three may be able to be set, as I found out. Press fn and each of the function keys in turn (f1-f12) while looking at the kernel log:

tail -f /var/log/kern.log

If they are recognised, you will see a kernel scancode:

atkbd serio0: Unknown key pressed (translated set 2, code 0xa7 on isa0060/serio0).   
kernel: [22751.972058] atkbd serio0: Use 'setkeycodes e027 <keycode>' to make it known.

Now, for example, you must set these key combinations up with the information given by the kernel- i.e use the kernel scancode and a keycode of your choice (preferably a high number so it doesn't interfere with anything else). See your current keycodes with sudo dumpkeys | grep ^keycode, as noted in this useful Ubuntu wiki article.)

It is clear that because fn is not recognised as a key, but only in combination with another key, the key combination fn + f3, for example, is treated in effect as a single key, and so can be set just like other single multimedia buttons:

sudo setkeycodes e027 135 

The above command must be entered separately for each of the key combinations you want to set, and must be placed in /etc/rc.local (without the sudo prefix) if you want them loaded on startup.

Now, you must check if there has been an X keysym associated with your new keycodes by going to keyboard shortcuts and seeing if the key combination, for example fn + F1, can be set. If it can be, a keysym such as XF86Paste will appear and the shortcut can be set to launch any program you wish.

Now all you need to do to test this with xte is to note the keysym (e.g. XF86Paste) you saw in the keyboard shortcuts dialogue for a particular combination (eg. fn + f1), and enter the appropriate command:

xte 'keydown XF86Paste' 'keyup XF86Paste'

This is the equivalent of you pressing and depressing fn + f1.

If you have set a program to launch with the key combination (fn + f...) assocated with the keysym, then the xte event will launch that program and you will know that the setting has worked.


Conclusion

This is a way to set fn + Function key combinations, but it will only be possible for your laptop if the kernel recognises at least one of the combinations as an unknown, but settable key. Only fn +f1, fn +f2, and fn + f3 were settable for me, but it will differ for your computer. If you want to experiment with modifying the actions of your 'sleep' fn + function key button, see this Ubuntu wiki article, but it may not be completely up to date.

Share:
6,421

Related videos on Youtube

komputes
Author by

komputes

Updated on September 18, 2022

Comments

  • komputes
    komputes over 1 year

    xte is part of the xautomation package. After hours of man page reading and looking for documentation or working examples, I still cannot get xte to emulate a Fn-F7 keypress.

    xte 'keydown Meta_L' 'key F7' 'keyup Meta_L'
    

    I've tried Meta_L, Meta_R, Multi_key, key F7, keydown F7 then keyup F7. No combination seems to work.

    Any idea how I can emulate a combination key press with a Fn key from a command/script?