Keyboard control patch for Pattern, from James H. [originally from svn r8435]