Yes.
The best way to do that is natively (not with Tasker).
/system/usr/keylayout/Vendor_????_Product_????.kl is the keylayout specific for your device.
You need to find out the VID/PID of your device (either Bluetooth or USB).
Code:
$ logcat|grep bt_btif (for Bluetooth)
$ lsusb -v (for wired USB)
Then you write your own keylayout and either put it in your Magisk module (best) or in the /system/usr/layout directly.