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.
It depends on the airplane, and there are speed limits on the ones that can be open. The windows on mine can be open up to 140 knots, but that is barely flying speed so it's really not practical.
Naturally, you can't be pressurized if you have a window open.