キーボード入力のフィルタリング

キーボード入力のフィルタリング

キーボードから入力を受け取り、素晴らしい視覚化を提供するプログラムがあります。このプログラムの目的は、私たちの赤ちゃんがキーボードで入力し、コンピュータに何かをさせることです。

ところで、メインプログラムとの接続が切れたキーボード入力殺菌プログラムを作成したいと思います。概念的に、私はプログラムが次のことをしたいと思います。

  sanitize_keyboard_input | my_program

my_programキーボードから入力を受けていると思いますが、実際にはから入力を受けていますsanitize_keyboard_input。これを行う方法はありますか?役立つ場合は、Ubuntu Linuxを実行しています。

ベストアンサー1

私はこれをずっと前に書いた。これは、ユーザー入力と対話型プログラムの間にあり、入力を傍受できるようにするスクリプトです。多くの問題を引き起こした古いFortranプログラムを実行すると、ファイル名を解決するためにシェルにエスケープするために使用されました。特定の入力を傍受して削除するように簡単に変更できます。

#!/usr/bin/perl

# shwrap.pl - Wrap any process for convenient escape to the shell.

use strict;
use warnings;

# Provide the executable to wrap as an argument
my $executable = shift;

my @escape_chars = ('#');             # Escape to shell with these chars
my $exit = 'bye';                     # Exit string for quick termination

open my $exe_fh, "|$executable @ARGV" or die "Cannot pipe to program $executable: $!";

# Set magic buffer autoflush on...
select((select($exe_fh), $| = 1)[0]);

# Accept input until the child process terminates or is terminated...
while ( 1 ) {
   chomp(my $input = <STDIN>);

   # End if we receive the special exit string...
   if ( $input =~ m/$exit/ ) {
      close $exe_fh;
      print "$0: Terminated child process...\n";
      exit;
   }

   foreach my $char ( @escape_chars ) {
      # Escape to the shell if the input starts with an escape character...
      if ( my ($command) = $input =~ m/^$char(.*)/ ) {
         system $command;
      }
      # Otherwise pass the input on to the executable...
      else {
         print $exe_fh "$input\n";
      }
   }
}

簡単なサンプルテストプログラムを試してみてください。

#!/usr/bin/perl

while (<>) {
   print "Got: $_";
}

おすすめ記事