def row_to_matrix(row, nkeys_per_row):
  """Convert keymap definition in a row to a matrix for easier indexing.

  Args:
    row: a list of key definitions.
    ncols_per_row: a list of numbers indicating number of keys in each
      row. The sum should add to the length of `row`.
  """
  assert sum(nkeys_per_row) == len(row)
  matrix = []
  idx = 0
  for nkeys in nkeys_per_row:
    matrix.append([row[idx + i] for i in range(nkeys)])
    idx += nkeys
  return matrix