I observed that the cockpit windows on the commercial airliners actually open - good for a hot day while waiting on the airfield, I suppose. Is there a mechanism which prevents the window from being opened when airborne?
I'm sure that the cabin pressure difference prevents this at altitude when the cabin is pressurized, due that they often slide inwards and then to the side.
That being said, Boeing produced a video showing that it's no problem flying with an open window on the 737 if say it opens on takeoff. You can close it assuming it's not damaged or you can land with it. They also showed that the side window can be opened in flight in case for instance visibility is impaired through the main window, and while it hardly looks comfortable, it can be done. This assumes that the cabin is unpressurized.
The sort of thing you might be thinking about is a cooper vane, but I don't think any aircraft have those on the cockpit windows.
Apparently the pilots of ET-363 on 2020-01-09 opened the cockpit window in flight to clean part of the windscreen from the remains of a locust swarm :
The windscreen wipers were not able to clear the windshield anymore. The crew went around, climbed to 8500 feet, depressurized the aircraft, opened the cockpit side window and cleaned the windscreen by hand. The same happened on second approach to Dire Dawa. The crew again climbed to 8500 feet, cleaned the windscreen by hand again and diverted to Addis Ababa.
The linked page contains a picture of the windscreens after "hitting" the locust swarm and the partial cleanup operation.