linux: detect if external keyboard is plugged to laptop

I have an OSX laptop with a running Ubuntu Virtualbox and I either interact with it with the laptop keyboard or with an external USB keyboard that I plug in when in the office.

Is there a script I could run to detect if the external keyboard is plugged so that the keymap in Ubuntu Virtualbox changes accordingly?

I currently manually run either ‘mac’ or ‘key’ which executes this command:

setxkbmap -layout gb

or

xmodmap .Xmodmap

I would like to do the same automatically when the keyboard is plugged or unplugged. Any ideas?